下面的代码将最后一个元素重复4次。选项卡式活动片段中使用了此ListView。我在互联网上搜索,但发现没有任何帮助。请帮忙!

我的适配器类:

public class MyAdapter extends ArrayAdapter<ElementList> {
public MyAdapter(Context context, ArrayList<ElementList> EventList){
    super(context,0,EventList);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent){


     ViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_adapter, parent, false);

        holder = new ViewHolder();
        holder.id = (TextView) convertView.findViewById(R.id.textView9);
        holder.name = (TextView) convertView.findViewById(R.id.textView6);
        holder.date =(TextView)convertView.findViewById(R.id.textView7);
        holder.time =(TextView)convertView.findViewById(R.id.textView8);

        convertView.setTag( holder );
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    ElementList elementList = getItem(position);

    if(elementList != null) {

        holder.id.setText(ElementList.id);
        holder.name.setText(ElementList.name);
        holder.date.setText(ElementList.date);
        holder.time.setText(ElementList.time);
    }

    return convertView;
}


该ListView的用法:

ArrayList<ElementList> EventList = new ArrayList<ElementList>();
EventList.add( new ElementList("1","bla1","12/34/45","12:40"));
                    EventList.add( new ElementList("1","bla2","12/34/45","12:40"));
                    EventList.add( new ElementList("1","bla3","12/34/45","12:40"));
                    EventList.add( new ElementList("1","bla4","12/34/45","12:40"));

                MyAdapter adapter = new MyAdapter(getActivity(),EventList);
                ListView myList = (ListView) rootView.findViewById(R.id.listView);
                myList.setAdapter(adapter);


有什么建议么 ? 。

最佳答案

我认为这

    if(elementList != null) {

    holder.id.setText(ElementList.id);
    holder.name.setText(ElementList.name);
    holder.date.setText(ElementList.date);
    holder.time.setText(ElementList.time);
    }


应该是这个

    if(elementList != null) {

    holder.id.setText(elementList.id);
    holder.name.setText(elementList.name);
    holder.date.setText(elementList.date);
    holder.time.setText(elementList.time);
}


不过,我不知道这是否会有所帮助。您可以添加一些日志语句,以在填充数组后以及适配器准备好从中读取时立即查看arrayList中的实际内容。

关于java - 我的自定义 ListView 适配器重复最后一项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41641177/

10-13 09:00