简体   繁体   English

如何在android中获取Autocompletetextview的所选项目的id?

[英]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.我的问题是当我从 autocompletetextview 中选择状态时,它总是返回 1 作为 id。 but i want to get the id accordingly to the state as per shown my json.但我想根据我的 json 中显示的状态获取相应的 id。 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.示例(StateName = Assam 然后 StateId = 4)。但我总是将 id 设为 1。我正在使用模型类来设置 id 并从中获取。但没有任何变化,我将 id 设为 1。如果有人知道我该如何解决这个问题。然后请告诉我。 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 :-这是我从 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 :-这是我的适配器,我在自动完成文本视图上应用 onItemclick 监听器:-

 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.您的代码使用在onItemClick回调中返回的i ,它指的是您从自动完成列表中的可见项目中单击的项目,而不是您的原始列表。 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 .当您单击自动完成列表中的第一项i=0 ,这意味着它始终返回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.在我的脑海中,您可以从mStateAdapter获取项目 String 并将其与您的mpojoStateList进行比较并找到相应的项目。 (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 .如果在您的PojoState对象中覆盖您的toString()方法并使其返回mStateName ,并将mpojoStateList传递给适配器,而不必创建 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 ).这样, mStateAdapter.getItem(i)将返回一个PojoState对象而不是一个字符串,并且您可以在不引用返回位置 ( i ) 的情况下使用它的 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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