Android: OnItemClickListener returns 最后一项的值
Android: OnItemClickListener returns the value of the last item
下面的代码对我来说工作得很好,但现在我不知道它有什么问题!!每当我单击一个项目时,它都会将最后一个项目的值用于意图!有什么想法吗?
protected void onPostExecute(List<HashMap<String, String>> result) {
customList=new ArrayList<>();
for (int i = 0; i < result.size(); i++) {
HashMap<String, String> appointement = result.get(i);
String fromT = appointement.get("fromT");
String toT = appointement.get("toT");
String date = appointement.get("date");
//int doctorid=Integer.parseInt(id.replaceAll("[\D]",""));
addAvailableAppoint(fromT, toT, date);
}
updateListView();
}
}
private void addAvailableAppoint(final String fromT, final String toT, final String date) {
customList.add(new AvailabilityList(fromT));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("items", "fromt " + fromT + "tot: " + toT);
Intent intent = new Intent(MakeAppointementActivity.this, AppointementActivity.class);
intent.putExtra("Doctor's name", DoctorName);
intent.putExtra("Doctor's infos", DoctorInfos);
intent.putExtra("fromT", fromT);
intent.putExtra("toT", toT);
intent.putExtra("date", date);
startActivity(intent);
}
});
}
// split new function for update listview
private void updateListView(){
ArrayAdapter adapter=new DoctorAvailabilityAdapter(MakeAppointementActivity.this,R.layout.list_items,customList);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
whenever i click to an item it put the value of the last item to the intent!
您忽略了 onItemClick()
的所有参数。如果您希望数据有所不同,则需要通过 position
或 id
.
在您的集合中找到合适的数据
1) 您不需要为列表中的每个新项目设置点击侦听器。
只需在 for 循环之外设置一次。
2) 尝试为包含所有数据的适配器使用模型 class:
fromT
、toT
和 date
。因为正如我所见,您的 AvailabilityList
仅包含 fromT
.
3) 如@CommonsWare 回答中所述,在 onItemClick
中尝试使用参数中的 position
或 id
找到合适的对象.
例如:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AvailabilityList item = (AvailabilityList)parent.getItem(position);
//Then from your item you can get the data: fromT , toT and date
}
下面的代码对我来说工作得很好,但现在我不知道它有什么问题!!每当我单击一个项目时,它都会将最后一个项目的值用于意图!有什么想法吗?
protected void onPostExecute(List<HashMap<String, String>> result) {
customList=new ArrayList<>();
for (int i = 0; i < result.size(); i++) {
HashMap<String, String> appointement = result.get(i);
String fromT = appointement.get("fromT");
String toT = appointement.get("toT");
String date = appointement.get("date");
//int doctorid=Integer.parseInt(id.replaceAll("[\D]",""));
addAvailableAppoint(fromT, toT, date);
}
updateListView();
}
}
private void addAvailableAppoint(final String fromT, final String toT, final String date) {
customList.add(new AvailabilityList(fromT));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("items", "fromt " + fromT + "tot: " + toT);
Intent intent = new Intent(MakeAppointementActivity.this, AppointementActivity.class);
intent.putExtra("Doctor's name", DoctorName);
intent.putExtra("Doctor's infos", DoctorInfos);
intent.putExtra("fromT", fromT);
intent.putExtra("toT", toT);
intent.putExtra("date", date);
startActivity(intent);
}
});
}
// split new function for update listview
private void updateListView(){
ArrayAdapter adapter=new DoctorAvailabilityAdapter(MakeAppointementActivity.this,R.layout.list_items,customList);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
whenever i click to an item it put the value of the last item to the intent!
您忽略了 onItemClick()
的所有参数。如果您希望数据有所不同,则需要通过 position
或 id
.
1) 您不需要为列表中的每个新项目设置点击侦听器。 只需在 for 循环之外设置一次。
2) 尝试为包含所有数据的适配器使用模型 class:
fromT
、toT
和 date
。因为正如我所见,您的 AvailabilityList
仅包含 fromT
.
3) 如@CommonsWare 回答中所述,在 onItemClick
中尝试使用参数中的 position
或 id
找到合适的对象.
例如:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AvailabilityList item = (AvailabilityList)parent.getItem(position);
//Then from your item you can get the data: fromT , toT and date
}