在我全部选中后,我在取消选中复选框时遇到了一些问题。
在这里,您是我的代码的简要概述。
我用一个名为“MyAdapter”的自定义适配器填充一个列表项,在这个适配器中我使用:
1)一个布尔数组来保存女巫复选框是checket而不是女巫
2)一个“Users”的ArrayList,其中User是一个包含许多信息,如电话号码和联系人姓名
3) 一个 ArrayList,我只添加了选中的用户。
问题是:如果我手动选中所有复选框,然后取消选中某些复选框,则一切正常。Vut 如果我使用 checkAll() 方法,我将很快向您展示,然后手动取消选中一些,它只能以图形方式工作(复选框未选中),但是未选中的用户不会从对应的数组列表中删除。
这是我的代码,任何后都将不胜感激:
public class MyAdapter extends ArrayAdapter<User> {
private boolean[] checkList;
private ArrayList<User> checkedUsersList;
private LinkedList<User> originalList;
private int listSize;
private Context mContext;
public MyAdapter(Context context,int textViewResourceId,
List<User> objects) {
super(context,textViewResourceId, objects);
mContext = context;
checkedUsersList = new ArrayList<User>();
originalList = (LinkedList<User>) objects;
checkList = new boolean[objects.size()];
listSize = objects.size();
for(int i=0;i<objects.size();i++)
{
checkList[i] = false;
}
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getViewOptimize(position, convertView, parent);
}
public View getViewOptimize(int position, View convertView, ViewGroup parent) {
final int pos = position;
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.rowcustom, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.textViewName);
viewHolder.number = (TextView)convertView.findViewById(R.id.textViewNumber);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBoxUserLine);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final User utente = getItem(position);
viewHolder.name.setText(utente.getContactName());
viewHolder.number.setText(utente.getContactNumber());
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Toast toast = Toast.makeText(mContext, "Check true notified", 1500);
toast.show();
if(!checkedUsersList.contains(utente))
checkedUsersList.add(utente);
}
else
{
Toast toast = Toast.makeText(mContext, "Check false notified", 1500);
toast.show();
checkedUsersList.remove(utente);
}
checkList[pos] = isChecked;
}
});
viewHolder.checkBox.setChecked(checkList[pos]);
return convertView;
}
public ArrayList<User> getUsersChecked()
{
return checkedUsersList;
}
public void checkAll()
{
for(int i = 0; i<listSize;i++)
{
checkList[i] = true;
}
checkedUsersList.clear();
for(User us : originalList)
{
checkedUsersList.add(new User(us.getContactName(), us.getContactNumber()));
}
notifyDataSetChanged();
}
private class ViewHolder {
public TextView name;
public TextView number;
public CheckBox checkBox;
}
}