我的问题是当我从autocompletetextview中选择状态时,它总是返回1作为id。但是我希望根据我的json显示相应的状态。示例(StateName = Assam然后StateId = 4)。但我总是将id设为1.我正在使用模型类设置id并从中获取。但是没有变化我将id作为1。如果有人知道我怎么能解决这个问题。那么请告诉我。提前谢谢。
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
},
这是我从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");
}
}
这是我的适配器,我在autocompltetextview上应用onItemclick listner: -
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();
}
});
答案 0 :(得分:0)
您的代码使用在i
回调中返回的onItemClick
,该回调是指您从自动完成列表中的可见项目中点击的项目,而不是原始列表。当您点击自动完成列表中的第一项i=0
时,这意味着它始终会返回“{em> Andaman和Nicobar Island ”项目StateId=1
。
在我的头顶,您可以从mStateAdapter
获取项目字符串,并将其与您的mpojoStateList
进行比较,然后找到相应的项目。 (查看示例代码)
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.
}
}
}
});
最好还是在PojoState
对象中覆盖toString()
方法并使其返回mStateName
,然后将mpojoStateList
传递给适配器必须做3 ArrayLists
。这样,mStateAdapter.getItem(i)
将返回PojoState
对象而不是String,您可以使用其ID而不引用返回的位置(i
)。