LoveAndroid LoveAndroid - 1 month ago 11
JSON Question

In AutoCompleteTextView apply on item click listner returns always id as a 1

This is my json responce :-

{
"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 my method by which i am fetching 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.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");

}
}


This is my Adapter where i am setting it to the 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();


}
});


My problem is autocompletetextview always return id 1.i am applying onitemclick listner on it.but it is getting id 1 always.i want to id accordingly to the state as per shown my json.Can anyone tell me how can I achieve this ??

Answer

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 mAdapter and compare it to your mpojoStateList and find the corresponding item. (Check the sample code)

    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.
                }
            }
        }
    });

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, mAdapter.getItem(i) will return a PojoState object instead of a String, and you can use its ID without referring to the returned position (i).