My situation: an activity with a button, a checkbox checkAll and a custom ListView with image,name,surname and checkbox
Problem: when i click on checkAll, the listView doesn't update the vale of each checkbox in the listView(whic have to be all checked).
Here there is my MainActivity with the 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);
}
});
This is my adapter with the getView function():
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;
}
}
With log i'm sure that when i click on checkAll all my items change their value in true (checked), but this is not show on the relative checkbox that stay all unchecked...
Thanks for the answer!!!
I'll suggest you to pass Person []
through your Adapter
's constructor like this:
public NewQAAdapterSelectFriends(Context context, Person[] p) {
mInflater = LayoutInflater.from(context);
this.data = p; //set class level variable.
}
wipe out all boolean[] checkBoxState;
from adapter, since you have state reference in person class i.e people.setCheck(true);
. so instead of using checkBoxState
use data[position].getCheck()
like this:
viewHolder.checkBox.setChecked(data[position].getCheck()); //to set check box state.
Remove setData(Person[] data)
method too, and hope it all will go well.