重复ListView中的项目?

时间:2014-06-02 12:54:35

标签: android android-listview custom-adapter

一切都运转正常,但现在我没有得到正在发生的事情,我在listView中显示了10个项目,它的工作正常,直到第6个项目,之后又从第1个项目再次重复??

    public class GroupDetailsAdapter extends BaseAdapter {
        List<GetSetGroupDetails> group_details = new ArrayList<GetSetGroupDetails>();
        Context context;
        Typeface face;

        public GroupDetailsAdapter(List<GetSetGroupDetails> group_details,
                Context context) {
            super();
            this.group_details.clear();
            this.group_details = group_details;
            this.context = context;
            face = Typeface.createFromAsset(context.getAssets(),
                    "HelveticaNeueLTStd-Th.otf");
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            int count = group_details.size();
            return count;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            View v = convertView;
            ViewHolder holder;
            if (v == null) {
                try {

                    LayoutInflater vi = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.group_item, null);
                    holder = new ViewHolder();

                    holder.tv_group_name = (TextView) v
                            .findViewById(R.id.tv_group_name);
                    String group_name = group_details.get(position).getGroup_name();
                    holder.tv_group_name.setText(group_name);
                    holder.tv_group_name.setTypeface(face);

                    holder.tv_group_reg_id = (TextView) v
                            .findViewById(R.id.tv_group_reg_id);
                    String groupRegId = group_details.get(position)
                            .getGroup_reg_id();
                    holder.tv_group_reg_id.setText(groupRegId);
                    holder.tv_group_reg_id.setTypeface(face);   

                    holder.tv_subscriber_count = (TextView) v
                            .findViewById(R.id.tv_subscriber_count);
                    holder.tv_subscriber_count.setText(group_details.get(position)
                            .getSubscriber_count());

                    v.setTag(holder);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else
                holder = (ViewHolder) v.getTag();
            return v;

        }

        class ViewHolder {
            TextView tv_group_name;
            TextView tv_group_reg_id;
            TextView tv_subscriber_count;

        }

    }

2 个答案:

答案 0 :(得分:19)

您正面临数据重复问题,因为您没有遵循实施View-Holder模式的确切标准。

错误:

如果view为null,那么你正在执行findViewById()和设置数据,因此对于第一组项目它将正常运行,后面它将显示下一组项目的相同数据。

public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            View v = convertView;
            ViewHolder holder;
            if (v == null) {
                try {

                    LayoutInflater vi = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.group_item, null);
                    holder = new ViewHolder();

                    holder.tv_group_name = (TextView) v
                            .findViewById(R.id.tv_group_name);
                    String group_name = group_details.get(position).getGroup_name();
                    holder.tv_group_name.setText(group_name);
                    holder.tv_group_name.setTypeface(face);

                    holder.tv_group_reg_id = (TextView) v
                            .findViewById(R.id.tv_group_reg_id);
                    String groupRegId = group_details.get(position)
                            .getGroup_reg_id();
                    holder.tv_group_reg_id.setText(groupRegId);
                    holder.tv_group_reg_id.setTypeface(face);   

                    holder.tv_subscriber_count = (TextView) v
                            .findViewById(R.id.tv_subscriber_count);
                    holder.tv_subscriber_count.setText(group_details.get(position)
                            .getSubscriber_count());

                    v.setTag(holder);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else
                holder = (ViewHolder) v.getTag();
            return v;

        }

正确:

实现View Holder模式的正确方法是在当前视图为空(这将是第一次发生)时查找视图,并仅在执行后设置数据。因此,最终findViewById()进程将首次完成,下次将通过使用附加标记获取视图。

public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            View v = convertView;
            ViewHolder holder;
            if (v == null) {
                    LayoutInflater vi = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.group_item, null);

                    holder = new ViewHolder();

                    holder.tv_group_name = (TextView) v
                            .findViewById(R.id.tv_group_name);
                    holder.tv_group_reg_id = (TextView) v
                            .findViewById(R.id.tv_group_reg_id);
                    holder.tv_subscriber_count = (TextView) v
                            .findViewById(R.id.tv_subscriber_count);
                    v.setTag(holder);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else
                holder = (ViewHolder) v.getTag();

            String group_name = group_details.get(position).getGroup_name();
            holder.tv_group_name.setText(group_name);
            holder.tv_group_name.setTypeface(face);

            String groupRegId = group_details.get(position)
                            .getGroup_reg_id();
            holder.tv_group_reg_id.setText(groupRegId);
            holder.tv_group_reg_id.setTypeface(face);   

            holder.tv_subscriber_count.setText(group_details.get(position)
                            .getSubscriber_count());
            return v;

        }

答案 1 :(得分:-1)

你可能会得到nullpointerexception ..摆脱这个尝试这个简单的代码

 public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder vh;
            View v = convertView;
            if(v == null){

                v = LayoutInflater.from(ctx).inflate(R.layout.simple_list_item, parent, false);
                vh = new ViewHolder(v);
                v.setTag(vh);
            }
            else{
                vh = (ViewHolder) v.getTag();
            }

            vh.tvTitle.setText(list.get(position).toString());
            return v;
        }

        class ViewHolder{
            TextView tvTitle;

            public ViewHolder(View v){
                tvTitle = (TextView) v.findViewById(R.id.tvVideoName);
            }