在 ListActivity 中将复选框选定的联系人保存在哪里?
我正在使用自定义列表适配器来显示所有联系人的列表,然后用户将通过单击复选框来选择联系人。将所选联系人保存到首选项以便我可以在其他地方进行测试的好方法是什么?我正在考虑使用 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 Activity(为简洁起见,编辑了一些代码):
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();
}
}
I am using a custom List adapter to display a list of all contacts, and the user will then select contacts by clicking a checkbox. What is a good method of saving the selected contacts to preferences, so that I may test against them elsewhere? I was thinking of using SharedPreferences, but so far I have been unable to find a way to save an array to SharedPrefs. I could go the sqlite route, but that seems a bit excessive considering they are already contained in a db, and why couldn't I just reference them there. Just not sure how to even begin to do that...
Also, I was thinking of calling this method onDestroy, though that could have some issues as well, so a recommendation on that could help as well. Here is some code (let me know if you need more,,,there's always more)
thanks for your supreme knowledge.
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 Activity (some code redacted for brevity's sake):
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要的只是存储对象(用户)列表,我会跳过数据库,只创建一个 JSON 对象并将其作为字符串存储在共享首选项中。这显然取决于您期望存储的用户数量,但如果是几百或更少,您将不会看到任何性能损失,并且您可以避免处理所有数据库设置。
If all you need is to store a list of objects (users) I would skip the DB and just create a JSON object and store that as a String in a shared preference. It obviously depends on how many users you expect to store, but if it's a few hundred or less, you're not going to see any sort of performance loss, and you can avoid dealing with all the db setup.
是的,最好的方法是使用 sql 数据库。在 Android 中,您实际上只有两个持久数据存储选项:sqlite 和共享首选项*。由于您想要实际存储更复杂的单个键值对,因此最好的选择是使用 sqlite 数据库。至于何时存储数据,每次使用 OnCheckChangedListener。
*是的,还有其他选项,但这些是唯一使用起来比较简单的选项。
Yea, you're best route is to use a sql database. You only really have two options for persistent data storage in android: sqlite and shared preferences*. Since you want to actually store something more complicated a single key value pair, your best bet is to go with a sqlite database. As far as when to store the data, I would just trigger a store each time an item is checked or unchecked individually using an OnCheckChangedListener.
*Yes, there are other options, but those are the only semi-easy ones to use.