Display JSON Objects data in Android AutoCompleteTextview

81 Views Asked by At
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 is a string json object . setter and getter methods are defined in a TranslatorModel class. i want to display englishSentence values in my autocompleteTextView

Problem: Which code will be used for displaying data in autocompletetextview ? Where to add array adaptar class and which code will work ? Which code will be used onPostExecute Method ?

1

There are 1 best solutions below

1
On

add ArrayAdapter inside onPostExecute method

@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);
}