从JSON解析显示数据并使用Volley for Network在自动完成文本上显示数据

时间:2018-01-30 10:30:08

标签: android json autocompletetextview

我一直在尝试解析其余API中的json数据,但无法成功: 预期输出:一旦我在自动填充文本中输入名称,它应显示以该字符开头的最近名称或名称的列表,反之亦然。它应该像自动完成文本一样工作。 我做了很多研究。请帮助我。 我不知道如何在自动完成文本上显示json值。

我正在获取json输出,我使用StringRequest解析它但没有成功。

JSON:

[{"c_name":"Arafat"},{"c_name":"Krishna Naik"},{"c_name":"Samih kuttan"}]

构造函数代码:

private String name;

public ListCustomer(String name){
    this.name=name;
}

public String getName(){
    return name;
}
public void setName(String name) {
    this.name = name;
}`

StringRequest的Listname方法:

public void ListCustomernames(){
    StringRequest stringRequest = new StringRequest(Request.Method.GET,
            URLs.URL_listpopupwindow,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    try {
                        //converting the string to json array object
                        JSONArray array = new JSONArray(response);

                        List<String> responseList = new ArrayList<String>();
                        for(int i=0;i< array.length();i++){
                            //getting user object from json array

                            final JSONObject jsonObjec = array.getJSONObject(i);
                                    //jsonObjec.getString("c_name");
                            ListCustomer listCustomer=new ListCustomer(
                                    jsonObjec.getString("c_name"));

                            responseList.add(listCustomer);

                        }


                        ArrayAdapter<String> adapter = new ArrayAdapter<String> (getApplicationContext(),android.R.layout.simple_dropdown_item_1line, responseList);
                        mVoiceInputTv.setAdapter(adapter);
                        mVoiceInputTv.setThreshold(1);
                        //ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,);
                       // mVoiceInputTv.setAdapter(adapter);

                    }

                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //stopping the swipeRefeshLayout
                    //swipeRefreshLayout.setRefreshing(false);
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError  volleyError) {
                    Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_SHORT).show();
                    //swipeRefreshLayout.setRefreshing(false);
                }
            });
    RequestQueue requestQueue= Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

2 个答案:

答案 0 :(得分:0)

尝试GSON它真正简化了JSON解析

答案 1 :(得分:0)

你应该这样做。                        JSONArray array = new JSONArray(response);

                    List<String> responseList = new ArrayList<String>();
                    String name;
                    for(int i=0;i< array.length();i++){                         
                        name = array.getJSONObject(i).getString("c_name");                             
                        responseList.add(name);
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String> (getApplicationContext(),android.R.layout.simple_dropdown_item_1line, responseList);
                    mVoiceInputTv.setAdapter(adapter);

你也可以这样做

  List<String> responseList = new ArrayList<String>();
                String name;
                for(int i=0;i< array.length();i++){                         
                    final JSONObject jsonObjec = array.getJSONObject(i);
                                //jsonObjec.getString("c_name");
                        ListCustomer listCustomer=new ListCustomer(
                                jsonObjec.getString("c_name"));                         
                    responseList.add(listCustomer.getName());
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String> (getApplicationContext(),android.R.layout.simple_dropdown_item_1line, responseList);
                mVoiceInputTv.setAdapter(adapter);

最好的办法是

List<String> responseList = new ArrayList<String>();
            String name;
            List<ListCustomer> listnames=new ArrayList();
            for(int i=0;i< array.length();i++){                         
                final JSONObject jsonObjec = array.getJSONObject(i);
                            //jsonObjec.getString("c_name");
                    listnames.setName(jsonObjec.getString("c_name"));                         
                responseList.add(listnames.get(i).getName());
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String> (getApplicationContext(),android.R.layout.simple_dropdown_item_1line, responseList);
            mVoiceInputTv.setAdapter(adapter);