ListView Custom Adapter repeat items

余生长醉 提交于 2019-12-11 05:47:33

问题


Whats wrong with this adapter, when i scroll down i see repeated rows at the bottom and then when scroll up again i see also repeated rows at the top that were not exist before, and the rest of Data items does not appear

the adapter:

public class ClassesListViewAdapter extends BaseAdapter {
    private Context mContext;
    ArrayList<String> Data = new ArrayList<>();


    public ClassesListViewAdapter(Context context, ArrayList<String> data) {
        Data = data;
        mContext = context;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return Data.size();
    }
    private class ViewHolder{

        TextView ClassDataTV;

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.classes_list_view_item, parent, false);
            holder = new ViewHolder();
            holder.ClassDataTV = (TextView) convertView.findViewById(R.id.ClassDataTV);
            holder.ClassDataTV.setText(Data.get(position));
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }

        return convertView;
    }
}

and here how i use it:

    ArrayList<String> v = new ArrayList<>();
    v.add("AAAAAAA");
    v.add("WWWWWwW");
    v.add("VVVVVVV");
    v.add("SSSSSSSSS");
    v.add("QQQQQQQQQ");
    v.add("YYYYYYYY");
    v.add("TTTTTTT");
    v.add("UUUUUUUUUU");
    v.add("zzzzzzzzzzzz");
    v.add("CCCCCCCCCC");
    v.add("HHHHHHHHHHH");
    v.add("IIIIIIIIII");
    v.add("PPPPPPPPP");
    mListView.setAdapter(new ClassesListViewAdapter(getActivity(), v));

回答1:


Put following part of your code outside if-block and it will be fixed :

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.classes_list_view_item, parent, false);
            holder = new ViewHolder();
            holder.ClassDataTV = (TextView) convertView.findViewById(R.id.ClassDataTV);

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

      // initialize your view here 
        holder.ClassDataTV.setText(Data.get(position));
        return convertView;
    }

The logic behind ViewHolder pattern tells that you should do it in this way. when you scroll some of reference will not created again and else block called so this cause your list not updated as you expected.



来源:https://stackoverflow.com/questions/37037746/listview-custom-adapter-repeat-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!