0

I have an Autocomplete text view in an Android activity, the view gets data from an arrayadapter from json array. I want to get the original JSON array id when someone selects an option in the autocomplete text view. I am getting the selected string but I wan the actual ID not the selected pos id Here is my code:

private void LoadSearchData() {

    RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest=new StringRequest(Request.Method.GET, url_class.Search_Dist, new Response.Listener<String>() {

        @Override

        public void onResponse(String response) {

            try {

                JSONObject jsonObject=new JSONObject(response);
                JSONArray jsonArray=jsonObject.getJSONArray("place_info");
                data_list=new String[jsonArray.length()];
                for (int i=0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject1=jsonArray.getJSONObject(i);
                    String dis_name=jsonObject1.getString("store_name" );
                    String dis_id=jsonObject1.getString("store_id");
                       Toast.makeText(getApplicationContext(),"District name="+dis_name,Toast.LENGTH_SHORT).show();
                       district_name.add(dis_name);
                     ///  district_whole.add(dis_name+"-"+dis_id);
                       data_list[i]=dis_name+"-"+dis_id;
                }
                //spinner_search.setAdapter(new ArrayAdapter<String>(Dashboard.this, android.R.layout.simple_spinner_dropdown_item, district_name));

                autoCompleteTextView.setAdapter(new ArrayAdapter<String>(Dashboard.this, android.R.layout.simple_spinner_dropdown_item, district_name));


            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }, new Response.ErrorListener() {

        @Override

        public void onErrorResponse(VolleyError error) {
            MDToast mdToast=MDToast.makeText(getApplicationContext(), "Something went wrong!!", Toast.LENGTH_SHORT, MDToast.TYPE_WARNING);
            mdToast.show();
            error.printStackTrace();

        }

    });

    int socketTimeout=30000;

    RetryPolicy policy=new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

    stringRequest.setRetryPolicy(policy);

    requestQueue.add(stringRequest);


}

OnSelectItemlistner:

autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String place_name=autoCompleteTextView.getText().toString();
            int txt_indx =i;

            //Toast.makeText(getApplicationContext(),"ID="+i,Toast.LENGTH_SHORT).show();
           // Toast.makeText(getApplicationContext(),"ID="+i,Toast.LENGTH_SHORT).show();
            GetstockInfo(place_name);
            textViewDist.setText(place_name);
        }
    });
  • Create an [POJO](https://stackoverflow.com/questions/12517905/what-is-java-pojo-class-java-bean-normal-class) class that represents an item in your json response, pass that list of pojos to the adapter. – JakeB Dec 10 '19 at 10:00

3 Answers3

0

go through this link

AutoCompleteTextView detect when selected entry from list edited by user

Subrata Das
  • 135
  • 1
  • 12
0

First of all, this is not recommended. But if you want then give a try

if(data_list.length > i) {
    String id = data_list[i].split("-")[1];
}

Suggestion: You should use a custom adapter with custom object to achieve this

Custom model:

class Store {
    String name;
    String id;

    // getter - setter
}

Custom Adapter:

class StoreAdapter extends ArrayAdapter<Store> {

}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

Create a POJO class matching the data from your json response

public class Store {
    private final String storeId;
    private final String storeName;

    public Store(String storeId, String storeName) {
        this.storeId = storeId;
        this.storeName = storeName;
    }

    public String getStoreId() {
        return storeId;
    }

    public String getStoreName() {
        return storeName;
    } 
}

Construct a list of Store's in your response method:

private final List<Store> stores = new ArrayList<>();

...

public void onResponse(String response) {

        try {

            JSONObject jsonObject=new JSONObject(response);
            JSONArray jsonArray=jsonObject.getJSONArray("place_info");
            for (int i=0; i < jsonArray.length(); i++) {
                JSONObject jsonObject1=jsonArray.getJSONObject(i);
                final String dis_name=jsonObject1.getString("store_name" );
                final String dis_id=jsonObject1.getString("store_id");

                stores.add(new Store(dis_id, dis_name));
            }

            // Create your custom adapter here and pass it "List<Store> stores"
            autoCompleteTextView.setAdapter(stores);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

You will also need to create a custom adapter class for your Spinner. There are plenty of tutorials for custom adapters.

JakeB
  • 2,043
  • 3
  • 12
  • 19