【发布时间】:2016-01-01 03:57:17
【问题描述】:
我有一个使用自定义适配器的 ListView。每行包含 2 个复选框,我想检查它们是否被勾选。
我将如何遍历每一行来检查这一点,然后,如果它们被选中,将行位置的布尔数组更改为 true,然后使用 SharedPreferences 保存这些数组?
在再次打开活动后,我将如何通过查看该位置的数组是真还是假来设置每个 CheckBox? 谢谢。
列表适配器
public class LockerAdapter extends ArrayAdapter<LockerData> {
private Context mContext;
private int mLayoutResourceId;
LockerData[] mData = null;
CheckBox checkBoxHwk;
CheckBox checkBoxLkr;
//trying to change these arrays if boxes are ticked
public boolean[] homeworkCheck = new boolean[6];
public boolean[] lockerCheck = new boolean[6];
public LockerAdapter(Context context, int resource, LockerData[] objects) {
super(context, resource, objects);
this.mContext = context;
this.mLayoutResourceId = resource;
this.mData = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LockerHolder holder = null;
if(row == null){
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId,parent,false);
holder = new LockerHolder();
holder.theLessonName = (TextView) row.findViewById(R.id.generalLessonName);
holder.hwkCheck = (CheckBox) row.findViewById(R.id.homeworkCheck);
holder.lkrCheck = (CheckBox) row.findViewById(R.id.lockerCheck);
row.setTag(holder);
}else {
holder = (LockerHolder) row.getTag();
}
LockerData lesson = mData[position];
holder.theLessonName.setText(lesson.lessonsName);
return row;
}
public class LockerHolder{
TextView theLessonName;
CheckBox hwkCheck;
CheckBox lkrCheck;
}
}
活动
public class HomeworkLockerActivity extends Activity {
private ListView hwkListview;
private LockerAdapter lockerAdapter;
private CheckBox checkBoxChem;
private CheckBox checkBoxPhys;
private CheckBox checkBoxMech;
private CheckBox checkBoxFP;
private CheckBox checkBoxCore;
private CheckBox checkBoxStats;
/*LockerData is just a class that takes in a single string in the
constructor to store the data*/
public LockerData[] lessons = {
new LockerData("Chemistry"),
new LockerData("Physics"),
new LockerData("Mechanics"),
new LockerData("Further Pure"),
new LockerData("Core 1"),
new LockerData("Statistics"),
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_homework_locker);
hwkListview = (ListView) findViewById(R.id.homeworkListview);
lockerAdapter = new LockerAdapter(getApplicationContext(),R.layout.list,lessons);
hwkListview.setAdapter(lockerAdapter);
}
}
【问题讨论】:
标签: android arrays listview for-loop