当我向下或向上滚动时,检查位置每次都会改变

时间:2015-07-09 13:08:42

标签: java android

当我检查任何项目(如位置0)时,它会自动随机检查任何项目(如位置7),当我向下或向上滚动时,检查位置每次都会改变...我厌倦了这个问题...?

public class contactAdpter extends ArrayAdapter<ContactItem> {

  Context context;
  List<ContactItem> list;

  public contactAdpter(Context context, int resource, List<ContactItem> items) {

    super(context, resource,items);

    this.context = context;
    this.list = items;

  }

  static class ViewHolder {

    ImageView imageview;

    TextView tv1;

    TextView tv2;

    CheckBox ch;

  }


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

  }


  @Override
  public int getPosition(ContactItem item) {
    return super.getPosition(item);

  }

  @Override
  public long getItemId(int id) {
    return id;

  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {


    ViewHolder holder=null;

    View view = convertView;


    ContactItem contactItem = getItem(position);


    LayoutInflater mInflater = (LayoutInflater) context
             .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);


    if (convertView == null) {

      view = mInflater.inflate(R.layout.contact_list, null);

      holder = new ViewHolder();

      holder.tv1 = (TextView) view.findViewById(R.id.name);
      holder.tv2 = (TextView) view.findViewById(R.id.number);
      holder.ch = (CheckBox) view.findViewById(R.id.checkbox1);

      view.setTag(holder);

    } else {

      holder = (ViewHolder) view.getTag();

    }

    holder.tv1.setText(contactItem.getConatct_name());
    holder.tv2.setText(contactItem.getNumber());

    return view;

  }
}

2 个答案:

答案 0 :(得分:0)

你必须在ContactItem中添加另一个变量,如下所示:

private boolean isSelected;

在适配器中生成此变量的getter和setter方法:

public class contactAdpter extends ArrayAdapter {

Context context;
List<ContactItem> list;



public contactAdpter(Context context, int resource, List<ContactItem> items) {


    super(context, resource,items);


    this.context = context;
    this.list = items;


}


static class ViewHolder {


    ImageView imageview;

    TextView tv1;

    TextView tv2;

    CheckBox ch;


}


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

}




@Override
public int getPosition(ContactItem item) {


    return super.getPosition(item);

}

@Override
public long getItemId(int id) {

    return id;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {



        ViewHolder holder=null;

        View view = convertView;


       ContactItem contactItem = getItem(position);


      LayoutInflater mInflater = (LayoutInflater) context
             .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);


        if (convertView == null) {

            view = mInflater.inflate(R.layout.contact_list, null);

            holder = new ViewHolder();

            holder.tv1 = (TextView) view.findViewById(R.id.name);
            holder.tv2 = (TextView) view.findViewById(R.id.number);
            holder.ch = (CheckBox) view.findViewById(R.id.checkbox1);


            view.setTag(holder);


        } else {


            holder = (ViewHolder) view.getTag();


        }



       holder.tv1.setText(contactItem.getConatct_name());
       holder.tv2.setText(contactItem.getNumber());

       if(contactItem.isSelected){
       holder.ch.isChecked(true);
       } else {
       holder.ch.isChecked(false);
       }

        holder.ch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
     if(contactItem.isSelected){
       holder.ch.isChecked(false);
       contactItem.setChecked(false);
       } else {
       holder.ch.isChecked(true);
        contactItem.setChecked(true);
       }
       notifyDatasetChanged();
            }
        });


        return view;

     }
}

答案 1 :(得分:0)

在ContactItem中

,您需要维护Checked Status。并按如下方式更改getView,

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

      ViewHolder holder=null;
      View view = convertView; 
      ContactItem contactItem = getItem(position);
      LayoutInflater mInflater = (LayoutInflater) context
             .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
           view = mInflater.inflate(R.layout.contact_list, null);
            holder = new ViewHolder();
            holder.tv1 = (TextView) view.findViewById(R.id.name);
            holder.tv2 = (TextView) view.findViewById(R.id.number);
            holder.ch = (CheckBox) view.findViewById(R.id.checkbox1);
            view.setTag(holder);
        } else {
          holder = (ViewHolder) view.getTag();
        }

       holder.tv1.setText(contactItem.getConatct_name());
       holder.tv2.setText(contactItem.getNumber());

       holder.ch.setOnCheckedChangeListener(null);
       holder.ch.setChecked(contactItem.getCheckedStatus());
       holder.ch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    contactItem.setCheckedStatus(isChecked);
                }
            });
       return view;

     }