我会在每个列表中创建包含复选框和textview的列表视图。我面临的问题是,我无法从适配器类中检索已检查和未检查值的数组。我在这里附上了某种代码。 这是我的Adapter类
public class MyFacilityAdapter<T> extends BaseAdapter {
ArrayList<T> mFList;
LayoutInflater mInflate;
SparseBooleanArray mySaprseBooleanArray;
ArrayList<Integer> items = new ArrayList<Integer>();
public MyFacilityAdapter(Context mContext, ArrayList<T> facilityList) {
// TODO Auto-generated constructor stub
mInflate = LayoutInflater.from(mContext);
mySaprseBooleanArray = new SparseBooleanArray();
mFList = new ArrayList<T>();
this.mFList = facilityList;
}
/*public ArrayList<T> getCheckeditems() {
ArrayList<T> tempArray = new ArrayList<T>();
for (int i = 0; i < mFList.size(); i++) {
if (mySaprseBooleanArray.get(i)) {
tempArray.add(mFList.get(i));
}
}
return tempArray;
}*/
ArrayList<Integer> getAdapterItems() {
return items;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mFList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mFList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int postion, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflate.inflate(R.layout.row_facility, null);
}
TextView tvFName = (TextView) convertView
.findViewById(R.id.tv_facilityName);
tvFName.setText(mFList.get(postion).toString());
CheckBox chkBox = (CheckBox) convertView.findViewById(R.id.chkBox);
chkBox.setTag(postion);
/*chkBox.setChecked(mySaprseBooleanArray.get(postion));*/
chkBox.setOnCheckedChangeListener(mChkChangedListener);
return convertView;
}
OnCheckedChangeListener mChkChangedListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
CheckBox chk = (CheckBox) buttonView;
// TODO Auto-generated method stub
if (chk.isChecked()) {
items.set(chk.getTag(), 1);
} else {
items.set(chk.getTag(), 0);
}
/*mySaprseBooleanArray.put((Integer) buttonView.getTag(), isChecked);*/
}
};
}
我将代码修改为[item.set(chk.getTag(),1)]。但是一旦我选中了复选框,就会触发Indexoutofbounds异常。我附在这里logcat。
04-02 01:21:53.002: E/AndroidRuntime(1019): FATAL EXCEPTION: main
04-02 01:21:53.002: E/AndroidRuntime(1019): Process: com.example.homeyapp, PID: 1019
04-02 01:21:53.002: E/AndroidRuntime(1019): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-02 01:21:53.002: E/AndroidRuntime(1019): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
04-02 01:21:53.002: E/AndroidRuntime(1019): at java.util.ArrayList.set(ArrayList.java:481)
04-02 01:21:53.002: E/AndroidRuntime(1019): at com.example.homeyapp.MyFacilityAdapter$1.onCheckedChanged(MyFacilityAdapter.java:95)
04-02 01:21:53.002: E/AndroidRuntime(1019): at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
04-02 01:21:53.002: E/AndroidRuntime(1019): at android.widget.CompoundButton.toggle(CompoundButton.java:87)
答案 0 :(得分:1)
想出来。 首先将整数1或0放入SparseBoolean数组中,如:
if (chk.isChecked()) {
mySaprseBooleanArray.put((Integer) chk.getTag(), isChecked);
} else {
mySaprseBooleanArray.put((Integer) chk.getTag(), isChecked);
}
然后, 为了获得已检查和未选中的项目数组:
public ArrayList<Integer> getCheckeditems() {
ArrayList<Integer> items = new ArrayList<Integer>();
for (int i = 0; i < mFList.size(); i++) {
Boolean temp = mySaprseBooleanArray.get(i);
if (temp) {
items.add(1);
} else {
items.add(0);
}
}
return items;
}
这里的物品返回100101010; 1表示已选中,0表示未选中。
答案 1 :(得分:0)
&#39;&#39;&#39; ArrayList需要使用与列表中的行数相同的元素预先填充。 (现在,每次用户检查或取消选中任何行时,您都会将 new 整数元素附加到ArrayList - 这没有任何意义。)
如果您执行上述操作,则onCheckChanged()可能如下所示:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CheckBox chk = (CheckBox) buttonView;
if (chk.isChecked()) {
items.set(chk.getTag(), 1);
} else {
items.set(chk.getTag(), 0);
}
/*mySaprseBooleanArray.put((Integer) buttonView.getTag(), isChecked);*/
}