JSON-array-auto-vervollständigen-text-anzeigen in android

arbeite ich an einer android app, wo ich Parsen eines Json-array und anpassen, um eine auto-vervollständigen-text-Ansicht. Ich bin immer die richtige Antwort von meinem server, da ich ein frischer android-und Json-parsing-ich bin nicht immer, wie zu analysieren, die json-array, array-adapter und fügen Sie es auf auto-vervollständigen-text-Ansicht.

Kann jeder Körper mir empfehlen, wie Sie es tun..?

Antwort wie folgt.

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

möchte ich Bereich ergänzt werden, in meinem auto die komplette text-Ansicht.

Ich erstellt automatisch komplette text-Ansicht in meine layout-Datei.

Dies ist, wie ich versucht bin,

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

Anregungen sind erwünscht...

InformationsquelleAutor rubin | 2014-04-15

Schreibe einen Kommentar