如何在Android中的文本框中填充建议列表

时间:2016-06-27 20:04:21

标签: android mysql android-studio

我有一个数组" array_topic_names"一些字符串值(由数据库检索),我想将其填充为文本框中的建议列表。

Android代码:

public class create_form extends Activity implements OnItemSelectedListener, AdapterView.OnItemClickListener {

private AutoCompleteTextView edttxt1 = null;
    private AutoCompleteTextView edt1 = null;
    private AutoCompleteTextView edt3 = null;
    protected void onCreate(Bundle savedInstanceState)

    {
      edttxt1 = (AutoCompleteTextView) findViewById(R.id.edttxt4);//topic1
      edt1 = (AutoCompleteTextView) findViewById(R.id.edttxt2);//topic2new PostAsync2().execute();
      super.onCreate(savedInstanceState);
      edt3 = (AutoCompleteTextView) findViewById(R.id.edttxt45);//topic3

      setContentView(R.layout.activity_create_form);

      adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, array_topic_names);

        edttxt1.setThreshold(1);
        //Set adapter to AutoCompleteTextView
        edttxt1.setAdapter(adapter2);
        edttxt1.setOnItemSelectedListener(this);
        edttxt1.setOnItemClickListener(this);

        edt1.setThreshold(1);
        //Set adapter to AutoCompleteTextView
        edt1.setAdapter(adapter2);
        edt1.setOnItemSelectedListener(this);
        edt1.setOnItemClickListener(this);

        edt3.setThreshold(1);
        //Set adapter to AutoCompleteTextView
        edt3.setAdapter(adapter2);
        edt3.setOnItemSelectedListener(this);
        edt3.setOnItemClickListener(this);
}
class PostAsync2 extends AsyncTask<String, String, JSONObject> {

        JSONParser jsonParser = new JSONParser();

        private ProgressDialog pDialog;

        private static final String LOGIN_URL = "http://10.0.3.2/topic_array_populate.php";


        private static final String TAG_SUCCESS = "success";
        private static final String TAG_MESSAGE = "message";


        @Override
        protected void onPreExecute() {
            Log.d("preexec", "starting");

        }

        @Override
        protected JSONObject doInBackground(String... args) {

            try {

                HashMap<String, String> params = new HashMap<>();

                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);


                if (json != null) {
                    Log.d("JSON result", json.toString());
                    return json;
                }



            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(JSONObject json) {

            int success = 0;
            String message = "";

            Log.d("post exec", "starting");

            Log.d("json", json.toString());

            if (pDialog != null && pDialog.isShowing()) {
                pDialog.dismiss();
            }

            if (json != null) {
                {
                    Log.d("json", "not null");

                    JSONArray data = null;
                    try {
                        data = json.getJSONArray("details");

                        Log.d("JSON result",json.toString());
                        for(int i=0;i<100;i++) {
                            JSONObject obj = data.getJSONObject(i);
                            Log.d("obj_value", obj.toString());
                            array_topic_names[i]=obj.toString().substring(15).replace("}","").replace("\"","");
                            Log.d("array_topic_names", array_topic_names[i]);
                        }



                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    success = json.getInt(TAG_SUCCESS);
                    message = json.getString(TAG_MESSAGE);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }


    }
}

logcat的

06-27 13:52:47.812 11218-11218/com.example.somya.client_feedback_application D/post exec: starting
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/json: {"details":[{"topic_name":"abc"},{"topic_name":"xyz"},{"topic_name":"bbb"},{"topic_name":"ccc"},{"topic_name":"1a"},{"topic_name":"2b"},{"topic_name":"3c"}]}
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/json: not null
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/JSON result: {"details":[{"topic_name":"abc"},{"topic_name":"xyz"},{"topic_name":"bbb"},{"topic_name":"ccc"},{"topic_name":"1a"},{"topic_name":"2b"},{"topic_name":"3c"}]}
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"abc"}
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/array_topic_names: abc
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"xyz"}
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/array_topic_names: xyz
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"bbb"}
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/array_topic_names: bbb
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"ccc"}
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/array_topic_names: ccc
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"1a"}
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/array_topic_names: 1a
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"2b"}
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/array_topic_names: 2b
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"3c"}
06-27 13:52:47.814 11218-11218/com.example.somya.client_feedback_application D/array_topic_names: 3c

我希望数组array_topic_names中的每个值都填充为我的文本框中的建议。我该怎么办? 现在,没有错误,但也没有显示建议列表。 而且当硬编码那个数组时,它正在工作,在我从数据库中检索它之后,它无法工作。

0 个答案:

没有答案