Rpond,我没有覆盖 onResume,所以我认为这不是问题。这里是 Activity 和相关的布局供大家查看。在代码中,您会看到许多 Log.d 语句(有时甚至更多。)通过这些 Log 语句,我能够验证代码是否完全按照我的预期工作。另外,请注意我添加到每个 CheckBox 的 onCheckChangedListener。它所做的只是打印一条日志语句,告诉我其中一个复选框的状态已更改。正是通过这个,我能够确定在我的 onCreate 返回后 CheckBox 的状态正在改变。您将看到我如何在 onCreate 结束时调用 inspectCheckboxes()。由此产生的日志语句不是旋转后我的屏幕上显示的内容,
SelectItems.java:
公共类 SelectItems 扩展 Activity {
public static final int SUCCESS = 95485839;
public static final String ITEM_LIST = "item list";
public static final String ITEM_NAME = "item name";
// Save state constants
private final String SAVE_SELECTED = "save selected";
private DbHelper db;
private ArrayList<Long> selectedIDs;
ArrayList<CheckBox> cboxes = new ArrayList<CheckBox>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_items);
// Create database helper
db = new DbHelper(this);
db.open();
initViews(savedInstanceState);
examineCheckboxes();
}
private void initViews(Bundle savedState) {
initButtons();
initHeading();
initList(savedState);
}
private void initButtons() {
// Setup event for done button
Button doneButton = (Button) findViewById(R.id.done_button);
doneButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
done();
}
});
// Setup event for cancel button
Button cancelButton = (Button) findViewById(R.id.cancel_button);
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
cancel();
}
});
}
private void initHeading() {
String itemName = getIntent().getExtras().getString(ITEM_NAME);
TextView headingField = (TextView) findViewById(R.id.heading_field);
if (itemName.equals("")) {
headingField.setText("No item name!");
} else {
headingField.setText("Item name: " + itemName);
}
}
private void initList(Bundle savedState) {
// Init selected id list
if (savedState != null && savedState.containsKey(SAVE_SELECTED)) {
long[] array = savedState.getLongArray(SAVE_SELECTED);
selectedIDs = new ArrayList<Long>();
for (long id : array) {
selectedIDs.add(id);
}
Log.d("initList", "restoring from saved state");
logIDList();
} else {
selectedIDs = (ArrayList<Long>) getIntent().getExtras().get(
ITEM_LIST);
Log.d("initList", "using database values");
logIDList();
}
// Begin building item list
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout scrollContent = (LinearLayout) findViewById(R.id.scroll_content);
Cursor cursor = db.getAllItems();
startManagingCursor(cursor);
cursor.moveToFirst();
// For each Item entry, create a list element
while (!cursor.isAfterLast()) {
View view = li.inflate(R.layout.item_element, null);
TextView name = (TextView) view.findViewById(R.id.item_name);
TextView id = (TextView) view.findViewById(R.id.item_id);
CheckBox cbox = (CheckBox) view.findViewById(R.id.checkbox);
name.setText(cursor.getString(cursor
.getColumnIndexOrThrow(DbHelper.COL_ITEM_NAME)));
final long itemID = cursor.getLong(cursor
.getColumnIndexOrThrow(DbHelper.COL_ID));
id.setText(String.valueOf(itemID));
// Set check box states based on selectedIDs array
if (selectedIDs.contains(itemID)) {
Log.d("set check state", "setting check to true for " + itemID);
cbox.setChecked(true);
} else {
Log.d("set check state", "setting check to false for " + itemID);
cbox.setChecked(false);
}
cbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("onClick", "id: " + itemID + ". button ref: "
+ ((CheckBox) v));
checkChanged(itemID);
}
});
//I implemented this listener just so I could see when my
//CheckBoxes were changing. Through this I was able to determine
//that my CheckBoxes were being altered outside my own code.
cbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
Log.d("check changed", "button: " + arg0 + " changed to: "
+ arg1);
}
});
cboxes.add(cbox);
scrollContent.addView(view);
cursor.moveToNext();
}
cursor.close();
examineCheckboxes();
}
private void done() {
Intent i = new Intent();
i.putExtra(ITEM_LIST, selectedIDs);
setResult(SUCCESS, i);
this.finish();
}
private void cancel() {
db.close();
finish();
}
private void checkChanged(long itemID) {
Log.d("checkChaged", "checkChanged for: "+itemID);
if (selectedIDs.contains(itemID)) {
selectedIDs.remove(itemID);
} else {
selectedIDs.add(itemID);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
long[] array = new long[selectedIDs.size()];
for (int i = 0; i < array.length; i++) {
array[i] = selectedIDs.get(i);
}
outState.putLongArray(SAVE_SELECTED, array);
}
//Debugging method used to see what is in selectedIDs at any point in time.
private void logIDList() {
String list = "";
for (long id : selectedIDs) {
list += id + " ";
}
Log.d("ID List", list);
}
//Debugging method used to check the state of all CheckBoxes.
private void examineCheckboxes(){
for(CheckBox cbox : cboxes){
Log.d("Check Cbox", "obj: "+cbox+" checked: "+cbox.isChecked());
}
}
}
选择项目.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/heading_field"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginBottom="10dip" android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout android:id="@+id/button_layout"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true">
<Button android:id="@+id/done_button" android:layout_weight="1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Done" />
<Button android:id="@+id/cancel_button" android:layout_weight="1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
<ScrollView android:orientation="vertical"
android:layout_below="@id/heading_field" android:layout_above="@id/button_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:id="@+id/scroll_content"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</ScrollView>
item_element.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_marginRight="5dip" />
<TextView android:id="@+id/item_name" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="18sp"
android:layout_centerVertical="true" android:layout_toLeftOf="@id/checkbox"
android:layout_alignParentLeft="true" />
<TextView android:id="@+id/item_id" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:visibility="invisible" />