【问题标题】:Android Studio Spinner issue when retrieving data from MySQL database从 MySQL 数据库中检索数据时的 Android Studio Spinner 问题
【发布时间】:2016-05-26 07:38:09
【问题描述】:

代码中没有错误,但是没有出现微调器数据。检查 php 文件时,在桌面上成功检索数据。请就如何解决此问题提出建议。谢谢

MainActivity.java

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MainActivity extends ActionBarActivity {
ArrayList<String> listItems=new ArrayList<>();
ArrayAdapter<String> adapter;
Spinner sp;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sp=(Spinner)findViewById(R.id.spinner);
    adapter=new ArrayAdapter<String>      (this,R.layout.spinner_layout,R.id.txt,listItems);
    sp.setAdapter(adapter);

}
public void onStart(){
    super.onStart();
    BackTask bt=new BackTask();
    bt.execute();
}
private class BackTask extends AsyncTask<Void,Void,Void> {
    ArrayList<String> list;
    protected void onPreExecute(){
        super.onPreExecute();
        list=new ArrayList<>();
    }
    protected Void doInBackground(Void...params){
        InputStream is=null;
        String result="";
        try{
            HttpClient httpclient=new DefaultHttpClient();
            HttpPost httppost= new HttpPost("http://10.1.1.57/localhost/getPatientsInfo.php");
            HttpResponse response=httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            // Get our response as a String.
            is = entity.getContent();
        }catch(IOException e){
            e.printStackTrace();
        }

        //convert response to string
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                result+=line;
            }
            is.close();
            //result=sb.toString();
        }catch(Exception e){
            e.printStackTrace();
        }
        // parse json data
        try{
            JSONArray jArray =new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                JSONObject jsonObject=jArray.getJSONObject(i);
                // add interviewee name to arraylist
                list.add(jsonObject.getString("NAME"));
            }
        }
        catch(JSONException e){
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void result){
        listItems.addAll(list);
        adapter.notifyDataSetChanged();
    }
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.qi.listview.MainActivity">

<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</Spinner>

spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/txt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.qi.listview">

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

enter image description here

【问题讨论】:

  • 检查 list.size() in Toast i thin size in your list of 0

标签: java php android mysql android-spinner


【解决方案1】:

你的异步任务有错误,你有本地任务

ArrayList<String> list; // !?

但是你必须在任务中返回这个列表然后分配它。

private class BackTask extends AsyncTask<Void,Void,List<String>> {

和

protected List<String> doInBackground(Void...params){

在doInBackground中:

return list;

在 onPostExecute 中:

protected void onPostExecute(List<String> result){
    listItems.addAll(result);
    adapter.notifyDataSetChanged();
}

有意义吗? ))

【讨论】:

  • 我已尝试根据您的建议更改代码,但仍然无法正常工作...还有其他建议吗?
  • 当您在 listItems.addAll(result) 上执行后停止调试器时;结果是否包含数据?
  • 抱歉,我是一个初学者,你是如何按照上面的描述去做的?
  • 那么问题出在数据加载中)你可以放置几个断点并找到你有错误的地方)
猜你喜欢
  • 2015-12-28
  • 2010-11-09
  • 2013-03-24
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多