私の状況:ボタン、チェックボックスcheckAll、および画像、名前、名前、チェックボックスを含むカスタムListViewを使用したアクティビティ
問題: checkAllをクリックすると、listViewはlistViewの各チェックボックスの値を更新しません(すべてチェックする必要があります)。
これがlistViewを使った私のMainActivityです:
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 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;
}
}
ログを使用すると、checkAllをクリックすると、すべてのアイテムの値がtrue(チェック済み)に変更されると確信していますが、これは、すべてチェックされていない相対チェックボックスには表示されません...
答えてくれてありがとう!!!