[英]how to get id of selected item in autocompletetextview in android
[英]How to get the id of the selected item of the Autocompletetextview in android?
我的问题是当我从 autocompletetextview 中选择状态时,它总是返回 1 作为 id。 但我想根据我的 json 中显示的状态获取相应的 id。 示例(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");
}
}
这是我的适配器,我在自动完成文本视图上应用 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();
}
});
您的代码使用在onItemClick
回调中返回的i
,它指的是您从自动完成列表中的可见项目中单击的项目,而不是您的原始列表。 当您单击自动完成列表中的第一项i=0
,这意味着它始终返回StateId=1
的“安达曼和尼科巴岛”项。
在我的脑海中,您可以从mStateAdapter
获取项目 String 并将其与您的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
对象而不是一个字符串,并且您可以在不引用返回位置 ( i
) 的情况下使用它的 ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.