我正在使用自定义列表适配器来显示所有联系人的列表,然后用户将通过单击复选框来选择联系人。将所选联系人保存到首选项的好方法是什么,以便我可以在其他地方对它们进行测试?我正在考虑使用 SharedPreferences,但到目前为止,我一直无法找到将数组保存到 SharedPrefs 的方法。我可以走 sqlite 路线,但考虑到它们已经包含在数据库中,这似乎有点过分,为什么我不能在那里引用它们。只是不知道如何开始这样做......另外,我正在考虑调用这个方法 onDestroy,虽然这也可能有一些问题,所以对此的建议也会有所帮助。这是一些代码(如果您需要更多,请告诉我,总是有更多)感谢您的至高无上的知识。
ListItemLayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
android:includeFontPadding="false"
android:paddingBottom="4dip"
android:textSize="15sp"
android:textStyle="normal" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
/>
</RelativeLayout>
</TwoLineListItem>
ContactPicker 活动(为简洁起见,编辑了一些代码):
public class ContactPicker extends ListActivity implements Runnable{
private List<Contact> contacts = null;
private Contact con;
private ContactArrayAdapter cAdapter;
private ProgressDialog prog = null;
private Context thisContext = this;
private CheckBox c1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prog = ProgressDialog.show(this, "Contact List", "Getting Contacts", true, false);
Thread thread = new Thread(this);
thread.start();
final CheckBox c1 = (CheckBox)findViewById(R.id.checkbox);
}
public void run() {...
}
private List<Contact> fillContactsList() {...
}
private Handler handler = new Handler() {...
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TextView label = ((TwoLineListItem) v).getText2();
String phoneNumber = label.getText().toString();
}
}