如何将JSON文件中的ID和名称与Android Studio中onitemclicklistener中的position参数相关联? 我有一个包含id和名称的JSON数据文件,我想在自动完成搜索中将它们检索到listview onTextChangedQuery。我已经这样做了,但现在我无法将它与OnItemClickListener中各自的位置联系起来。 这是JSON文件
"data": [
{
"id": "26",
"fullname": "Abbas",
"address": "mingora"
},
{
"id": "28",
"fullname": "shakeel",
"address": "mingora"
},
{
"id": "29",
"fullname": "Fayaz",
"address": "Odigram"
},
{
"id": "30",
"fullname": "Fayaz",
"address": "Odigram"
},
{
"id": "31",
"fullname": "m fayaz",
"address": "odigram"
}
]
以下是代码:
public void onResponse(String response) {
try {
JSONObject Jobject = new JSONObject(response);
JSONArray DATA = Jobject.getJSONArray("data");
for (int i = 0; i < DATA.length(); i++) {
jsonObject = DATA.getJSONObject(i);
id = jsonObject.getString("id");
name = jsonObject.getString("fullname");
// getting id from json
String newId = id;
int currentid = myids.length;
int newSize1 = currentid + 1;
String[] ArrayId = new String[newSize1];
for (int k = 0; k < currentid; k++) {
ArrayId[k] = myids[k];
}
ArrayId[newSize1 - 1] = newId;
myids = ArrayId;
//the following code adds new item to the array from the json object
String newItem = name;
int currentSize = contactList.length;
int newSize = currentSize + 1;
String[] tempArray = new String[newSize];
for (int j = 0; j < currentSize; j++) {
tempArray[j] = contactList[j];
}
tempArray[newSize - 1] = newItem;
contactList = tempArray;
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(BloodDonerActivity.this, R.layout.list_item, R.id.textView, contactList);
lv.setAdapter(arrayAdapter);
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
String userId = myids[pos];
Toast.makeText(getApplicationContext(), userId, Toast.LENGTH_SHORT).show();
//id of user on position
}
});