I have been trying to parse the json data from my rest API but could not succeed: Expected output: once I enter name in the autocomplete text, it should display a list of nearest name or names starting with that character and vice versa. It should work like auto complete text. I have done a lot of research. pls help me out. I dont know how to show the json value on autocomplete text.
I am getting json output, I parse the it using StringRequest but with no succes.
JSON:
[{"c_name":"Arafat"},{"c_name":"Krishna Naik"},{"c_name":"Samih kuttan"}]
Constructor code:
private String name;
public ListCustomer(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}`
Listname method for StringRequest:
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);
}
You Should probably be done like this. 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);
And also you can do like this
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);
Best way to do is
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);