如何在AutoCompleteTextView中显示建议列表并从api中填充数据

时间:2014-03-31 06:12:50

标签: android json autocomplete adapter

AutoCompleteTextView不显示任何建议,但数据已填入适配器

这是我的 CODE

public class AUTOSuggestion extends AsyncTask<String, String, String> {
    String TITLE, id,level;     
    String response = " ";
    ProgressDialog dialogProgress = new ProgressDialog(Home.this);

    String tex;

    public AUTOSuggestion(String text) {
        // TODO Auto-generated constructor stub
        tex=text;
    }

    protected void onPreExecute() {
        dialogProgress.setCancelable(true);
        dialogProgress.setMessage("Please wait..");
        dialogProgress.setIndeterminate(false);
        dialogProgress.show();

    }

    @Override
    protected String doInBackground(String... params) {
        try {
            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("search", tex));
        /*  param.add(new BasicNameValuePair("catId", id));
            param.add(new BasicNameValuePair("level", level));*/
            response = CustomHttpClient.executeHttpPost(AUTOSUGGESTION_URL, param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    protected void onPostExecute(String response)
    {
        if (response != "" || response != null) {
            try {
                ArrayList<String>sug_list=new ArrayList<String>();
                JSONObject Obj = new JSONObject(response);
                //String status = Obj.getString("TAG_STATUS");
                JSONArray jarr=Obj.getJSONArray("0");
                for(int i=0;i < jarr.length(); i++){
                JSONObject p = (JSONObject) jarr.get(i);
                String words = p.getString("title");
                sug_list.add(words);
                }

                item = sug_list.toArray(new String[sug_list.size()]);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getBaseContext(), e + "", Toast.LENGTH_LONG).show();
                finish();
            }

        }
        try {
            dialogProgress.dismiss();
        } catch (Exception e) {
            e.fillInStackTrace();
        }
        autocomplete = (AutoCompleteTextView) findViewById(R.id.autocomplete);
        //Toast.makeText(getBaseContext(), item.toString(), Toast.LENGTH_LONG).show();
        adapter = new ArrayAdapter<String>(Home.this,android.R.layout.simple_dropdown_item_1line, item);

        // Create adapter
        //adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, item);
        autocomplete.setThreshold(1);
        autocomplete.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

调试时显示

字样

GUYS请帮助我:'(

enter image description here

正如其他人所说,这是我所做的改变

修改后的代码

    public class AUTOSuggestion extends AsyncTask<String, String, String> {
    String TITLE, id,level;     
    String response = " ";
    ProgressDialog dialogProgress = new ProgressDialog(Home.this);

    String tex;

    public AUTOSuggestion(String text) {
        // TODO Auto-generated constructor stub
        tex=text;
    }

    protected void onPreExecute() {
        dialogProgress.setCancelable(true);
        dialogProgress.setMessage("Please wait..");
        dialogProgress.setIndeterminate(false);
        dialogProgress.show();

    }

    @Override
    protected String doInBackground(String... params) {
        try {
            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("search", tex));
            response = CustomHttpClient.executeHttpPost(AUTOSUGGESTION_URL, param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    protected void onPostExecute(String response)
    {
        if (response != "" || response != null) {
            try {

                JSONObject Obj = new JSONObject(response);
                //String status = Obj.getString("TAG_STATUS");
                JSONArray jarr=Obj.getJSONArray("0");
                for(int i=0;i < jarr.length(); i++){
                JSONObject p = (JSONObject) jarr.get(i);
                String words = p.getString("title");
                sug_list.add(words);
                }

                item = sug_list.toArray(new String[sug_list.size()]);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getBaseContext(), e + "", Toast.LENGTH_LONG).show();
                finish();
            }

        }
        try {
            dialogProgress.dismiss();
        } catch (Exception e) {
            e.fillInStackTrace();
        }

        adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,sug_list);         
        autocomplete.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        Toast.makeText(getBaseContext(),sug_list+"",Toast.LENGTH_LONG).show();
    }
}

这是item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#eaeaea"
android:padding="10dp"
android:textColor="#000"
android:textSize="16sp" >

</TextView>

enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

花了6个小时后,我才知道我必须添加一行

autocomplete.showDropDown();
<{1}} onpost

中的

答案 1 :(得分:-3)

检查此page

您可能需要update用户界面。

runOnUiThread(new Runnable(){
        public void run(){
             aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,suggest);
             autoComplete.setAdapter(aAdapter);
             aAdapter.notifyDataSetChanged();
        }
    });