在AutoCompleteTextView上应用于项目单击列表器,返回始终id为1

时间:2016-10-10 13:14:29

标签: android json android-volley autocompletetextview

这是我的json回复: -

{
  "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
    },

这是我从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.setmStateId(stateId);
                                    pojoState.setmStatename(stateName);
                                    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");

        }
    }

这是我的适配器,我将其设置为autocompletetextview: -

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

                mpojoStateList.get(i).getmStateId();


            }
        });

我的问题是autocompletetextview总是返回id 1.我在它上面应用onitemclick listner。但它正在获得id 1 always.i想要相应地显示我所显示的json状态。任何人都告诉我如何才能实现这?? ??

1 个答案:

答案 0 :(得分:0)

您的代码使用在i回调中返回的onItemClick,该回调是指您从自动完成列表中的可见项目中点击的项目,而不是原始列表。当您点击自动完成列表中的第一项i=0时,这意味着它始终返回&#34; 安达曼和尼科巴岛&#34;项目StateId=1

在我的头顶,您可以从mAdapter获取项目字符串,并将其与您的mpojoStateList进行比较,然后找到相应的项目。 (查看示例代码)

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

            String itemName = mAdapter.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.
                }
            }
        }
    });

最好还是在PojoState对象中覆盖toString()方法并使其返回mStateName,然后将mpojoStateList传递给适配器必须做3 ArrayLists。这样,mAdapter.getItem(i)将返回PojoState对象而不是String,您可以使用其ID而不引用返回的位置(i)。