Somnath Pal
Somnath Pal

Reputation: 1512

set jsonarray list into autocompletetextview

I'm trying to fetch list of services from web service and add it to autocompletetextview. So suppose if you enter "a", you will get the below list of services in the autocompletetextview. I think its a json array but I'm getting error "Json error: Value Body Spa at 0 of type java.lang.String cannot be converted to JSONArray". Please correct me if I'm doing this the wrong way.

// you get this json response on entering "a"

[
  "Body Spa",
  "Hair Cut",
  "Hair massage",
  "fghfghfgh",
  "rtyrt",
  "Gold Facial",
  "Foot's massage"
]
public class HomeOptionTwo extends Fragment implements TextWatcher{

    AutoCompleteTextView autoservice;
    String[] list;
    ArrayAdapter<String> adapter;

    private static final String SERVICE = "http://192.168.200.1/android/spaservice";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.homeoptiontwo, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        autoservice = (AutoCompleteTextView)view.findViewById(R.id.service);


        autoservice.addTextChangedListener(this);





    }

    private void prepareMyList() {

        StringRequest stringRequest = new StringRequest(Request.Method.POST, SERVICE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            //JSONObject jObj = new JSONObject(response);

                            JSONArray jsonarray = new JSONArray(response);
                            for (int i = 0; i < jsonarray.length(); i++) {
                                Toast.makeText(getActivity(), "services:" + jsonarray.getJSONArray(i), Toast.LENGTH_SHORT).show();

                                adapter = new ArrayAdapter<String>(
                                        getActivity(),
                                        android.R.layout.simple_dropdown_item_1line,
                                        list);

                                autoservice.setAdapter(adapter);
                            }





                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("'phrase", autoservice.getText().toString());
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(stringRequest);
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        prepareMyList();

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}

Upvotes: 0

Views: 1478

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

Here:

 Toast.makeText(getActivity(), 
     "services:" + jsonarray.getJSONArray(i), Toast.LENGTH_SHORT).show();

jsonarray contains String as item instead of JSONArray. use optString or getString to get value from jsonarray JSONArray using index. like:

ArrayList<String> arrList=new ArrayList<String>();
for (int i = 0; i < jsonarray.length(); i++) {
   String strValue=jsonarray.optString(i);                             
   arrList.add(strValue);
 }
  adapter = new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_dropdown_item_1line,
                arrList);

  autoservice.setAdapter(adapter);

Upvotes: 1

Related Questions