适配器正在重复行

时间:2014-11-14 17:19:01

标签: java android android-listview android-adapter

我有这个重复行的适配器,有没有人知道如何解决这个问题,为什么会发生这种情况?

这是我的适配器类:

public class CustomUsersAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;

    private ArrayList<UserInfoVO> mData = new ArrayList<UserInfoVO>();
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();

    private LayoutInflater mInflater;

    public CustomUsersAdapter(Context context) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final UserInfoVO item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSectionHeaderItem(final UserInfoVO item) {
        mData.add(item);
        sectionHeader.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public UserInfoVO getItem(int position) {
        return mData.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int rowType = getItemViewType(position);

//        if (convertView == null) {

        holder = new ViewHolder();
        switch (rowType) {
            case TYPE_ITEM:
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.members_list_item, null);
                    holder = new ViewHolder();
                    holder.userImage = (CXRemoteImageView) convertView.findViewById(R.id.members_item_pic);
                    holder.title = (CXRobotoTextView) convertView.findViewById(R.id.members_item_title);
                    holder.small_text = (CXRobotoTextView) convertView.findViewById(R.id.members_item_small_text);
                    holder.userImage.loadImmediate(mData.get(position).getPictureUrl());
                    holder.title.setText(mData.get(position).getName());
                    holder.small_text.setText(mData.get(position).getAboutMe());
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                break;
            case TYPE_SEPARATOR:
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.members_category_item, null);
                    TextView category = (TextView) convertView.findViewById(R.id.members_item_category);
                    category.setText(mData.get(position).getName());
                    convertView.setTag(category);
                } else {
                    TextView category=(TextView) convertView.getTag();
                }
                break;
        }

//            convertView.setTag(holder);
//        } else {
//            try {
//                holder = (ViewHolder) convertView.getTag();
//            }catch (Exception e)
//            {
//                e.printStackTrace();
//            }
//        }

        return convertView;
    }

    static class ViewHolder {
        public CXRemoteImageView userImage;
        public CXRobotoTextView title;
        public CXRobotoTextView small_text;
    }

}

以下是我如何填充它:

boolean members = false;
            boolean friends = false;

            usersAdapter=new CustomUsersAdapter(getActivity());
            for (int i = 0; i < rFArray.size(); i++) {
                if (rFArray.get(i).getName().equalsIgnoreCase("Members") || rFArray.get(i).getName().equalsIgnoreCase("My Friends")) {
                    if (rFArray.get(i).getName().equalsIgnoreCase("Members")) {
                        if (!members) {
                            members = true;
                            usersAdapter.addSectionHeaderItem(rFArray.get(i));
                        }

                    }
                    if (rFArray.get(i).getName().equalsIgnoreCase("My Friends")) {

                        if (!friends) {
                            friends = true;
                            usersAdapter.addSectionHeaderItem(rFArray.get(i));
                        }
                    }
                } else {
                    usersAdapter.addItem(rFArray.get(i));
                }
            }



            list.setAdapter(usersAdapter);

2 个答案:

答案 0 :(得分:3)

您正在重复行,因为您错误地处理了convertView不是null的情况。在这种情况下,你实际上还必须填写小部件。您要跳过的只是inflate()调用,因为您正在回收现有行。在您的情况下,您在回收时正在跳过所有

答案 1 :(得分:1)

最有可能的问题出在标有“ - &gt;”的地方:

switch (rowType) {
            case TYPE_ITEM:
                if (convertView == null) {
                   ...
                } else {
                    holder = (ViewHolder) convertView.getTag();
-->                    SET HERE YOUR VIEWS VALUES!
                }
                break;
            case TYPE_SEPARATOR:
                if (convertView == null) {
                   ...
                } else {
                    TextView category=(TextView) convertView.getTag();
-->                    SET HERE YOUR VIEWS VALUES!
                }
                break;
        }

您重复使用旧的convertView及其子视图保留其先前的值
您应该在每次getView()电话上设置其值

修改
实际上,您可以优化此代码 - 无条件地设置视图值 - 就在if-else子句

之后