試試這個。如果你得到一個沒有設置的,返回false。如果他們都經過測試返回true:
private boolean allChecked()
{
for(Object item : dataList)
{
if (!(SampleData)item.isSelected())
return false;
}
return true;
}
注意,DataList控件被定義爲Object類型的ArrayList,而不是類型的sampleData。因此,當您迭代每個條目時,需要將其轉換爲SampleData。
編輯:
要使用此,更改活動類,以便「適配器」是一個類變量:
public class CheckboxListActivity extends ListActivity {
//Define adapter here so that you can refer to it anywhere within the Activity
CheckboxListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
//Set the class level 'adapter' variable
adapter = new CheckboxListAdapter(getLayoutInflater());
getListView().setAdapter(adapter);
}
}
在你的onClick中,調用可變「適配器」的新方法:
public void onClickNeste_sjekkliste (View v) {
boolean ifCheckedAll = adapter.allChecked();
if (ifCheckedAll == false) {
Log.d("CheckedAll", "false");
}
else if (ifCheckedAll == true) {
Log.d("CheckedAll", "true");
}
}
請添加一些代碼來幫助我們來幫助您。 – deW1