简体   繁体   中英

How to get the id of the selected item of the Autocompletetextview in android?

My question is when i select the state from the autocompletetextview it always return 1 as an id. but i want to get the id accordingly to the state as per shown my json. Example (StateName = Assam then StateId = 4).but i am always getting id as 1. i am using model class to set the id and get from it.but there is no change i am getting the id as a 1. If anyone know how can i resolve this problem.then please tell me. thanks in advance.

This is my jsonResponce :-   

 {
      "ReplyCode": 1,
      "Message": "Franchisee and Plans List",

     "data2": [
        {
          "StateId": 1,
          "StateName": "Andaman and Nicobar Island",
          "CountryId": 1
        },
        {
          "StateId": 2,
          "StateName": "Andhra Pradesh",
          "CountryId": 1
        },
        {
          "StateId": 3,
          "StateName": "Arunachal Pradesh",
          "CountryId": 1
        },
        {
          "StateId": 4,
          "StateName": "Assam",
          "CountryId": 1
        },

This is the method by which i am getting the data from the json :-

public void volleyStatedata() {

        if (mGeneralUtilities.isConnected()) {
            mProgressDialog.show();
            StringRequest stateRequest = new StringRequest(Request.Method.POST, GlobalData.REGISTER_DATA_URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {


                            mProgressDialog.dismiss();

                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                JSONArray jsonArray = jsonObject.getJSONArray("data2");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    PojoState pojoState = new PojoState();
                                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                                    String stateId = jsonObject1.getString("StateId");
                                    String stateName = jsonObject1.getString("StateName");
                                    mStateList.add(stateName);
                                    mStateIdList.add(stateId);
                                    pojoState.setmStateList(mStateList);
                                    pojoState.setmStateId(stateId);
                                    mpojoStateList.add(pojoState);



                                }


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {

                            Log.e("error", "" + volleyError.getMessage());


                        }
                    }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {

                    Map<String, String> params = new HashMap<String, String>();


                    return params;
                }
            };

            RequestQueue stateQueue = Volley.newRequestQueue(getContext());

            stateQueue.add(stateRequest);
        } else {

            mGeneralUtilities.showAlertDialog("Hey User !", "Please connect to the internet", "Ok");

        }
    }

And this is my adapter where i am applying onItemclick listner on the autocompltetextview :-

 ArrayAdapter<String> mStateAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, mStateList);
        mActState.setAdapter(mStateAdapter);
        mActState.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                mpojoStateList.get(i).getmStateId();


            }
        });

Your code uses i that returns in the onItemClick callback, which refers to the item you clicked from the visible items in the auto-complete list, not your original list. When you click on the first item in the auto-complete list, i=0 , which means it always returns the " Andaman and Nicobar Island " item whose StateId=1 .

Off the top of my head, you can get the item String from the mStateAdapter and compare it to your mpojoStateList and find the corresponding item. (Check the sample code)

    final ArrayAdapter<String> mStateAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, mStateList);
    mActState.setAdapter(mStateAdapter);
    mActState.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            String itemName = mStateAdapter.getItem(i);

            for (PojoState pojo : mpojoStateList) {
                if (pojo.mStateName.equals(itemName)) {
                    String id = pojo.getmStateId(); // This is the correct ID
                    break; // No need to keep looping once you found it.
                }
            }
        }
    });

It also is better if, inside your PojoState object, you override your toString() method and make it return the mStateName , and pass the mpojoStateList to the adapter without having to make 3 ArrayLists . That way, mStateAdapter.getItem(i) will return a PojoState object instead of a String, and you can use its ID without referring to the returned position ( i ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM