我的情况:带有按钮的活动,复选框checkAll以及包含图片,姓名,姓氏和复选框的自定义ListView
问题:当我点击checkAll时,listView不会更新listView中每个复选框的值(必须全部选中)。
这里有我的MainActivity listView:
private final static int INFO_DIALOG = 1;
private ListView mList;
private CheckBox checkAll;
private Button buttonCreaPartita;
private Person[] people;
private int peopleSize;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout_select_friend);
mList= (ListView) findViewById(R.id.list); //the listView
NewQAAdapterSelectFriends adapter= new NewQAAdapterSelectFriends(this);
//my adapter
MainActivity.this.people = new Person[]{ //add person to my array of Person
new Person(1,"Belen", "Rodriguez", R.drawable.belen,false),
new Person(2,"Cameron", "Diaz", R.drawable.cameron,false),
new Person(3,"Jessica", "Alba", R.drawable.alba,false),
new Person(4,"Yolanthe", "Cabau", R.drawable.cabau,false),
new Person(5,"Belen", "Rodriguez", R.drawable.belen,false),
new Person(6,"Cameron", "Diaz", R.drawable.cameron,false)
};
peopleSize=people.length;
adapter.setData(people); //pass the array of person to my adapter
mList.setAdapter(adapter); //set the adpater
checkAll =(CheckBox)findViewById(R.id.checkBoxAll);//my CheckAll checkbox
checkAll.setChecked(false);//default value
checkAll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if((checkAll.isChecked())){
for(int i=0;i<peopleSize;i++){
people[i].setCheck(true);
//update the new value on the array
((BaseAdapter) mList.getAdapter()).notifyDataSetChanged() ; //i'll try to refresh the value of checkbox in the listview
}
}
else if(!(checkAll.isChecked())){
for(int i=0;i<peopleSize;i++){ people[i].setCheck(false);
((BaseAdapter) mList.getAdapter()).notifyDataSetChanged() ;
}
}
}
});
}
buttonNext=(Button)findViewById(R.id.button);
buttonNext.setOnClickListener(new OnClickListener() {
//after user select the person on the list press buttonNext to go to next Activity
@Override
public void onClick(View arg0) {
Person[] selectedFriends=new Person[peopleSize];
int countSelected=0;
for(int i=0;i<peopleSize;i++){
if(people[i].isCheck()){
selectedFriends[countSelected]=people[i];
countSelected++;
}
}
Toast.makeText(getApplicationContext(), "Numero elemento:"+countSelected, Toast.LENGTH_SHORT).show();
Intent intentMenu=new Intent(getApplicationContext(), MenuActivity.class);
Toast.makeText(getApplicationContext(), "Partita Creata", Toast.LENGTH_LONG).show();
startActivity(intentMenu);
}
});
这是我的getView函数()的适配器:
public class NewQAAdapterSelectFriends extends BaseAdapter {
private LayoutInflater mInflater;
private Person[] data;
boolean[] checkBoxState;
ViewHolder viewHolder;
public NewQAAdapterSelectFriends(Context context) {
mInflater = LayoutInflater.from(context);
}
public void setData(Person[] data) {
this.data = data;
checkBoxState=new boolean[data.length];
for(int i=0;i<data.length;i++){
checkBoxState[i]=data[i].isCheck();
}
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int item) {
return data[item];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_select_friends, null);
viewHolder=new ViewHolder();
viewHolder.nameText=(TextView) convertView.findViewById(R.id.personName);
viewHolder.surnameText=(TextView) convertView.findViewById(R.id.personSurname);
viewHolder.contactImage=(ImageView) convertView.findViewById(R.id.personImage);
viewHolder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.nameText.setText(data[position].getName());
viewHolder.surnameText.setText(data[position].getSurname());
viewHolder.contactImage.setImageResource(data[position].getPhotoRes());
viewHolder.contactImage.setScaleType(ScaleType.FIT_XY);
viewHolder.checkBox.setChecked(checkBoxState[position]);
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
checkBoxState[position]=true;
data[position].setCheck(true);
}else{
checkBoxState[position]=false;
data[position].setCheck(false);
}
}
});
viewHolder.checkBox.setChecked(checkBoxState[position]);
return convertView;
}
static class ViewHolder {
TextView nameText;
TextView surnameText;
ImageView contactImage;
CheckBox checkBox;
CheckBox checkAll;
}
}
使用日志我确定当我点击checkAll时,我的所有项目都会更改其值为true(已选中),但这不会显示在保持全部未选中的相对复选框上...
感谢您的回答!!!
答案 0 :(得分:1)
我建议你通过Person []
的{{1}}构造函数传递Adapter
:
public NewQAAdapterSelectFriends(Context context, Person[] p) {
mInflater = LayoutInflater.from(context);
this.data = p; //set class level variable.
}
从适配器中清除所有boolean[] checkBoxState;
,因为您在人员类中有状态引用,即people.setCheck(true);
。所以不要像这样使用checkBoxState
使用data[position].getCheck()
:
viewHolder.checkBox.setChecked(data[position].getCheck()); //to set check box state.
也删除setData(Person[] data)
方法,并希望一切顺利。