2012-07-06 70 views
-1

我的情況:一個按鈕,複選框checkAll與形象,名字,姓氏和複選框的自定義的ListView活動安卓:我的複選框「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(checked)的方式更改它們的值,但是這並不顯示在保持全部未選中的相對複選框中...

謝謝你的回答!

+0

[Android中有很多元素的列表中的CeckAll按鈕]的可能的重複(http://stackoverflow.com/questions/11319581/ceckall-button-in-a-list-with-a-lot-of -elements-in-android) – FoamyGuy 2012-07-06 14:39:43

+0

你是否嘗試設置監聽器OnCheckedChangeListener()? – NotCamelCase 2012-07-06 14:50:29

+0

我不認爲它會使它工作,如果不是,但我會將notifyDataSetChanged()移出for循環。這樣你只需更新適配器一次,可以提高性能。 – MinceMan 2012-07-06 15:06:23

回答

1

我會建議您通過Adapter的構造函數傳遞Person []這樣的:

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)法過了,希望這一切都會順利。

+0

謝謝你Adil:D ...你的建議完美解決了我的問題。 – 2012-07-06 15:46:34