AutoCompleteTextView с использованием JSON в Android


Я пытаюсь извлечь массив JSON в AutoCompleteTextView, но я получаю каждое значение дважды в раскрывающемся списке.

JSON:

{"names":[{"id":"1","names":"jacob"},{"id":"2","names":"amy"},{"id":"3","names":"melissa"}],"success":1,"message":"Successfully found "} 

Класс Ajax:

class Ajax5 extends AsyncTask<String, String, String> {// JSON STARTS HERE

    // new CreateNewProduct().execute();

    protected ArrayList<NameValuePair> parameters;
    JSONObject json;

    public Ajax5() {
        super();
        parameters = new ArrayList<NameValuePair>();

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    protected String doInBackground(String... args) {

        JSONParser jParser = new JSONParser();
        json = jParser
                .makeHttpRequest(
                        "http:link.php",
                        "GET", this.parameters);

        try {
            Log.d("Create Response", "four"
                    + json.getJSONObject("1").toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {
        try{
        JSONArray jArray=json.getJSONArray("names");
        pickname = new ArrayList<TreeMap<String, String>>();
        //TreeMap<String, TreeMap<String, String>> tempMap = new TreeMap<String, TreeMap<String, String>>();
        for(int i=0;i<jArray.length();i++){
            TreeMap<String,String> valuePairs=new TreeMap<String,String>();
            JSONObject tempObj=jArray.getJSONObject(i);
            valuePairs.put("key", tempObj.getString("names"));
            valuePairs.put("value", tempObj.getString("names"));
            pickpinn.add(valuePairs);

        }
                    }
         catch (JSONException e) {
                e.printStackTrace();
            }
        Log.d("name", pickpinn.toString());
        //routes.addAll(vehicles.values());
        SimpleAdapter sp5_adapter = new SimpleAdapter(OnlineBooking.this, pickpinn,
                R.layout.runner, new String[] { "key", "value" },

                new int[] { R.id.name_id, R.id.name_value });
        // sp1_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        actv1.setAdapter(sp5_adapter);

        actv1.addTextChangedListener(OnlineBooking.this);
        actv1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                    long arg3) {

                TextView stv = (TextView) v.findViewById(R.id.name_id);
                String route_id_string = stv.getText().toString();
                edsp5.setText(route_id_string);
                actv1.setText(((TextView) v.findViewById(R.id.name_value))
                        .getText().toString());


            }

        });

    }
}

Как это исправить? Спасибо.

1 2

1 ответ:

Вам нужно изменить это с

    JSONArray jArray=json.getJSONArray("names");
    pickname = new ArrayList<TreeMap<String, String>>();
    //TreeMap<String, TreeMap<String, String>> tempMap = new TreeMap<String, TreeMap<String, String>>();
    for(int i=0;i<jArray.length();i++){
        TreeMap<String,String> valuePairs=new TreeMap<String,String>();
        JSONObject tempObj=jArray.getJSONObject(i);
        valuePairs.put("key", tempObj.getString("names"));
        valuePairs.put("value", tempObj.getString("names"));
        pickpinn.add(valuePairs);

    }

К

     JSONArray jArray=json.getJSONArray("names");
     pickname = new ArrayList<TreeMap<String, String>>();
    //TreeMap<String, TreeMap<String, String>> tempMap = new TreeMap<String, TreeMap<String, String>>();
    for(int i=0;i<jArray.length();i++){
        TreeMap<String,String> valuePairs=new TreeMap<String,String>();
        JSONObject tempObj=jArray.getJSONObject(i);
        valuePairs.put("id", tempObj.getString("id"));
        valuePairs.put("names", tempObj.getString("names"));
        pickpinn.add(valuePairs);

    }

Также здесь

  SimpleAdapter sp5_adapter = new SimpleAdapter(OnlineBooking.this, pickpinn,
            R.layout.runner, new String[] { "key", "value" },

            new int[] { R.id.name_id, R.id.name_value });

К

  SimpleAdapter sp5_adapter = new SimpleAdapter(OnlineBooking.this, pickpinn,
            R.layout.runner, new String[] { "id", "names" },

            new int[] { R.id.name_id, R.id.name_value });