Listview,自定义适配器和复选框

时间:2014-04-08 10:01:14

标签: android checkbox android-listview adapter android-adapter

我的适配器出了问题。这是代码:

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    final ViewHolder viewHolder;
    View view = convertView;
    if (view == null) {
        view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
        viewHolder = new ViewHolder();

        viewHolder.textTitle = (TextView) view.findViewById(R.id.title);
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);

        viewHolder.checkBox.setTag(position);
        view.setTag(viewHolder);

        viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
    } else {
        viewHolder = (ViewHolder) view.getTag();
        viewHolder.checkBox.getTag(position);
    }

    viewHolder.textTitle.setText(getItem(position).getTitle());

    viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position));
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                myIntegerArrayList.add(position);
            } else {
                myIntegerArrayList.remove((Object) position);
            }
        }
    });

    return view;
}

private static class ViewHolder {
    TextView textTitle;
    CheckBox checkBox;
}

复选框本身工作正常..我的意思是,位置正确添加到我的arraylist但是如果(假设我检查位置0处的项目)我滚动到底部,然后,滚动回到顶部项目0返回未选中甚至如果该位置是我的arraylist(用日志验证)

3 个答案:

答案 0 :(得分:3)

viewHolder.checkBox.setTag(position);移出if并在position中使用viewHolder.checkBox.getTag();更改onCheckedChanged,因此您的代码必须如下:

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    final ViewHolder viewHolder;
    View view = convertView;
    if (view == null) {
        view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
        viewHolder = new ViewHolder();

        viewHolder.textTitle = (TextView) view.findViewById(R.id.title);
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);


        view.setTag(viewHolder);

        viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
    } else {
        viewHolder = (ViewHolder) view.getTag();
        viewHolder.checkBox.getTag(position);
    }
    viewHolder.checkBox.setTag(position);

    viewHolder.textTitle.setText(getItem(position).getTitle());

    viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position));
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                myIntegerArrayList.add(viewHolder.checkBox.getTag());
            } else {
                myIntegerArrayList.remove((Object) viewHolder.checkBox.getTag());
            }
        }
    });

    return view;
}

您需要移出viewHolder.checkBox.setTag(position);,因为Recycle ListView,您需要在标记中保存您的位置,以便在onCheckedChanged中获取该位置。

由于同样的原因,您需要在viewHolder.checkBox.getTag();中使用onCheckedChanged

很乐意提供帮助

答案 1 :(得分:0)

只需使用

CheckBoxInListHelper.java

public class CheckBoxInListHelper {
String check = null;
String name = null;
String image = null;
boolean selected = false;

public CheckBoxInListHelper(String check, String name, String image, boolean selected) {
    super();
    this.check = check;
    this.image = image;
    this.name = name;
    this.selected = selected;
}

public String getcheck() {
    return check;
}

public void setcheck(String check) {
    this.check = check;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getimage() {
    return image;
}

public void setimage(String image) {
    this.image = image;
}


public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

}

然后使用

调用set list view
 newmodelList = new ArrayList<CheckBoxInListHelper>();
  adapter=new FriendsListAdapter(FriendsList.this,newmodelList);                        
                fList.setAdapter(adapter);  

在FriendsListAdapter中

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;       
    if (convertView == null) {
        LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.friendslist_details, null);
        holder = new ViewHolder();
        holder.nameOfUser =(TextView)convertView.findViewById(R.id.name);
        holder.imageOfUser =(ImageView)convertView.findViewById(R.id.userImage);                
        holder.chkBox =(com.nuevalgo.groupbuy.CustomCheckBox)convertView.findViewById(R.id.chkbox);
        convertView.setTag(holder);

        holder.chkBox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                com.nuevalgo.groupbuy.CustomCheckBox cb = (com.nuevalgo.groupbuy.CustomCheckBox) v;
                CheckBoxInListHelper _state = (CheckBoxInListHelper) cb.getTag();                   
                _state.setSelected(cb.isChecked());                 


                FriendsList obj =   new FriendsList();
                obj.unCheck(activity);
            }
        });

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    CheckBoxInListHelper state = (CheckBoxInListHelper) getArrayList.get(position);

    if (position == 0 || position % 2 == 0){
        convertView.setBackgroundColor(Color.parseColor("#ffffff"));
    }
    else
    {
        convertView.setBackgroundColor(Color.parseColor("#f2f3f1"));
    }          
    holder.nameOfUser.setText(state.getName());         
    if(state.getimage()!=null)
    {
        imageLoader.get(state.getimage(), ImageLoader.getImageListener(holder.imageOfUser,
                R.drawable.loading, R.drawable.error));
    }
    holder.chkBox.setChecked(state.isSelected());
    holder.chkBox.setTag(state);
    return convertView;
}

答案 2 :(得分:0)

   //Try this
                    private boolean[] selection;   
                    thumbnailsselection = new boolean[myIntegerArrayList.size()];         

  //put below code in getview method                          

                    holder.checkbox.setId(position);
                    viewHolder.imageView .setId(position);



        holder.checkbox.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) {

                System.out.println("click-1");

                // TODO Auto-generated method stub
                CheckBox cb = (CheckBox) v;
                int id = cb.getId();
                if (selection[id])
                                    {
                    cb.setChecked(false);
                    selection[id] = false;
                } else {
                    cb.setChecked(true);
                    selection[id] = true;
                }
            }
        });

                    holder.checkbox.setChecked(selection[position]);
                    holder.id = position;