Listview, custom adapter and checkboxes

岁酱吖の 提交于 2020-01-02 10:27:22

问题


i got an issue with my adapter. Here is the code:

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

The checkbox itself works fine.. I mean, the position is correctly added to my arraylist but if (assuming i checked the item at position 0) i scroll to bottom, then, scroll back to top the item 0 return unchecked even if the position is into my arraylist (verified with logs)


回答1:


move out viewHolder.checkBox.setTag(position); from if and change position with viewHolder.checkBox.getTag(); in onCheckedChanged, so your code must be like:

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

you need move out viewHolder.checkBox.setTag(position); because Recycle ListView, you need save your position in Tag to getting that in onCheckedChanged.

you need use viewHolder.checkBox.getTag(); in onCheckedChanged because same reason.

glad to help




回答2:


just use

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

}

then call set list view using

 newmodelList = new ArrayList<CheckBoxInListHelper>();
  adapter=new FriendsListAdapter(FriendsList.this,newmodelList);                        
                fList.setAdapter(adapter);  

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



回答3:


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


来源:https://stackoverflow.com/questions/22933674/listview-custom-adapter-and-checkboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!