在 ListActivity 中将复选框选定的联系人保存在哪里?

发布于 2024-12-14 20:16:10 字数 2893 浏览 1 评论 0原文

我正在使用自定义列表适配器来显示所有联系人的列表,然后用户将通过单击复选框来选择联系人。将所选联系人保存到首选项以便我可以在其他地方进行测试的好方法是什么?我正在考虑使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳哥哥 2024-12-21 20:16:10

如果您需要的只是存储对象(用户)列表,我会跳过数据库,只创建一个 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.

倾城°AllureLove 2024-12-21 20:16:10

是的,最好的方法是使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文