Android:McClickListener返回最后一项的值

Android:McClickListener返回最后一项的值,android,listview,onitemclicklistener,onitemclick,Android,Listview,Onitemclicklistener,Onitemclick,贝娄密码对我来说很好,但现在我不知道它出了什么问题!!每当我点击一个项目时,它都会将最后一个项目的值添加到目标中!有什么想法吗 protected void onPostExecute(List<HashMap<String, String>> result) { customList=new ArrayList<>(); for (int i = 0; i < result.size(); i++) {

贝娄密码对我来说很好,但现在我不知道它出了什么问题!!每当我点击一个项目时,它都会将最后一个项目的值添加到目标中!有什么想法吗

 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);
受保护的void onPostExecute(列表结果){
customList=newarraylist();
对于(int i=0;i
每当我点击一个项目时,它都会将最后一个项目的值添加到目标中

您正在忽略
onimclick()
的所有参数。如果您希望数据发生变化,则需要通过
position
id

1)在集合中查找适当的数据。您不需要为列表中的每个新项目设置单击侦听器。 只需设置一次,在for循环之外

2)尝试为适配器使用包含所有数据的模型类:
fromT
toT
date
。因为正如我看到的,您的
可用性列表只包含
fromT

3)如@commonware answer中所述,在
onimclick
中,尝试使用参数中的
position
id
查找适当的对象

例如:

@覆盖
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
AvailabilityList项=(AvailabilityList)parent.getItem(position);
//然后从您的项目中可以获得数据:fromT、toT和date
}

我理解(1)和(3)。对于(2)在列表视图中,我只想通过intent显示fromT和send toT&date,这就是为什么!虽然在单击某个项目时要发送数据,但需要从该项目中获取全部数据,这就是为什么在模型中放置
fromT
toT
date
会更好的原因。--For(3):in
onItemClick()
方法,您拥有所单击项目的
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
    }