TheLettuceMaster
TheLettuceMaster

Reputation: 15744

Custom ListView Repeating Itself

Here is my getView() method:

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

        if (convertView == null) {

            switch (rowType) {


            case 1:

                convertView = mInflater.inflate(
                        R.layout.single_class_select_row_header, parent, false);
                holder = new ViewHolder();

                holder.label = (TextView) convertView
                        .findViewById(R.id.tvSingleClassSelectHeader);
                convertView.setTag(holder);
                break;
            case 2:

                final int newPos = getRealPosition(pos);
                convertView = mInflater.inflate(
                        R.layout.single_class_select_row, parent, false);
                holder = new ViewHolder();

                holder.checkBox = (CheckBox) convertView
                        .findViewById(R.id.chkSession);
                holder.mainLayout = (LinearLayout) convertView
                        .findViewById(R.id.llSingleClassLayout);

                holder.phase = (TextView) convertView.findViewById(R.id.phase);
                holder.class_info = (TextView) convertView
                        .findViewById(R.id.class_info);
                holder.phase.setText(getListObject(pos).phase);
                holder.class_info.setText(getListObject(pos).info);
                convertView.setTag(holder);

                holder.checkBox.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                            // edit out 
                        }
                    }
                });

                holder.mainLayout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                                             // edit out  
                                         }
                    }
                });

                holder.phase.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                                                   // edit out
                        }
                    }
                });


                break;
            }

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



        return convertView;
    }

Can anyone see why? I have looked at other examples, but because I am overriding getItemViewType() because of headers in the middle of the list, no examples match my circumstances since I am doing things differently.

It seems that I should put the setOnClickListner's below the if-block, but the I have problem with my holder variable not being initialized while other instances of it need it to be final.

Upvotes: 0

Views: 291

Answers (1)

user
user

Reputation: 87074

You need to set the data(text or what other piece of information you have) outside of the if block where you test the convertView for being null. Otherwise you'll set the data only when convertView is null and as this view will get recycled you'll end up with the same piece of information that was previously set.

Upvotes: 2

Related Questions