在Android AutoCompleteTextview中显示JSON对象数据

时间:2017-01-03 15:43:36

标签: json arraylist android-asynctask android-arrayadapter autocompletetextview

public class MainActivity extends AppCompatActivity {


    private AutoCompleteTextView autoCompleteTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);


        new HttpGetTask().execute("http://192.168.0.107/abc/translator.php");

    }

    public class HttpGetTask extends AsyncTask<String, String, List<TranslatorModel>> {


        @Override
        protected List<TranslatorModel> doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;


            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();

                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }

                String finalJson = buffer.toString();
                JSONArray parentArray = new JSONArray(finalJson);

                List<TranslatorModel> translatorModelList = new ArrayList<>();


                for(int i= 0; i<parentArray.length();i++) {

                    JSONObject finalObject = parentArray.getJSONObject(i);

                    TranslatorModel translatorModel = new TranslatorModel();


                        translatorModel.setEnglish(finalObject.getString("englishSentence"));


                        translatorModelList.add(translatorModel);
                    }



                return translatorModelList;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }

                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }


        @Override
        protected void onPostExecute(List<TranslatorModel> data) {

            super.onPostExecute(data);


        }
    }
}

englishSentence是一个字符串json对象。 setter和getter方法在TranslatorModel类中定义。我想在我的autocompleteTextView

中显示englishSentence值

问题: 哪个代码将用于在autocompletetextview中显示数据? 在哪里添加数组adaptar类以及哪些代码可以工作? 将在PostExecute方法中使用哪些代码?

1 个答案:

答案 0 :(得分:0)

在onPostExecute方法中添加ArrayAdapter

@Override
protected void onPostExecute(List<TranslatorModel> data) {

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);

    autoCompleteTextView.setAdapter(adapter);
    autoCompleteTextView.setThreshold(1);



super.onPostExecute(data);
}