Repeating items in ListView?

后端 未结 2 1318
心在旅途
心在旅途 2020-12-17 05:15

Everything was working fine but now I am not getting what is happening,I have 10 items to display in the listView,its working fine till the 6th item and after that it repeat

相关标签:
2条回答
  • 2020-12-17 05:47

    You may get nullpointerexception.. to get rid out of this try this simple code

     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);
                }
    
    0 讨论(0)
  • 2020-12-17 06:04

    You are facing that data repeat issue, just because you aren't following the exact standards of implementing View-Holder pattern.

    Wrong:

    Here you are doing findViewById() and setting data if view is null, so it will be running fine for first set of items, afterword it will show you the same data for the next sets of items.

    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;
    
            }
    

    Correct:

    Correct way to implement View Holder pattern is to find views if current view is null (that would happen for the first time) and set data only after doing it. So eventually findViewById() process will be done for the first time and next time onwards it will get views by using attached tags.

    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;
    
            }
    
    0 讨论(0)
提交回复
热议问题