JSON array to automatically end text view in Android

I am working on an Android application where I have to parse a Json array and adapt it to automatic full text representation. I get the correct answer from my server, since I am better versed in android and Json parsing. I am not getting how to parse this json array for an array adapter and add it to the automatic full text view.

Can any body suggest me how to do this ??

The answer is as follows.

[
{
city: "bng",
area: "anagar",
id: 510000032
},
{
city: "bng",
area: "bnagar",
id: 510000021
},
{
city: "bng",
area: "cnagar",
id: 510000037
}
] 

I want the region to be mapped in my automatic full text view.

I automatically created the full text in my layout file.

This is how i try

    public class MainActivity extends Activity {

     List<String> responseList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AutoCompleteTextView auto1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

        new HttpGetTask().execute();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_expandable_list_item_2, responseList);
        auto1.setAdapter(adapter);
    }

    private class HttpGetTask extends AsyncTask<Void, Void, String> {

        String URL = "http://xyz/cities.json?app_id=test";
        AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

        @Override
        protected String doInBackground(Void... params) {
            HttpGet request = new HttpGet(URL);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            try {
                return mClient.execute(request, responseHandler);
            } catch (ClientProtocolException exception) {
                exception.printStackTrace();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            try {
                JSONArray json = new JSONArray(result);
                Log.v("ResponseCity", result);

                responseList = new ArrayList<String>();

                for (int i = 0; i < json.length(); i++) {
                    final JSONObject e = json.getJSONObject(i);
                    String name = e.getString("area");
                    responseList.add(name);

                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (null != mClient)
                mClient.close();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Any suggestions are welcome ...

+4
3

List<String>,

 List<String> responseList = new ArrayList<String>();

 for (int i = 0; i < json.length(); i++) {
    final JSONObject e = json.getJSONObject(i);
     String name = e.getString("area");
     responseList.add(name);
  }

AutoCompleteTextView

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, responseList);
 AutoCompleteTextView textView = (AutoCompleteTextView)
            findViewById(R.id.autoCompleteTextView1);
 textView.setAdapter(adapter);
+9

String[] arr=new String[100]; 

. , ArrayList, , Getter/Setter. . .

:

ArrayList<String>arr=new ArrayList<>();

:

for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
                String imei = jsonObject1.getString("imei");
                String name = jsonObject1.getString("name");
                String my_pic = jsonObject1.getString("my_pic");
                String email = jsonObject1.getString("email");

                arr.add(name);
            }
            adapter= new ArrayAdapter<>
                    (this, android.R.layout.select_dialog_item, arr);
            autoCompleteText.setThreshold(1);//will start working from first character
            autoCompleteText.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
            autoCompleteText.setTextColor(Color.RED);
        }

,

0
source

Source: https://habr.com/ru/post/1536526/


All Articles