![](/img/trans.png)
[英]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.