2

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

2 回答 2

2

如果您只需要存储对象(用户)列表,我会跳过数据库,只需创建一个 JSON 对象并将其作为字符串存储在共享首选项中。这显然取决于您希望存储多少用户,但如果它是几百或更少,您不会看到任何类型的性能损失,并且您可以避免处理所有数据库设置。

于 2011-11-09T03:18:49.837 回答
1

是的,你最好的方法是使用 sql 数据库。对于 android 中的持久数据存储,您实际上只有两个选项:sqlite 和共享首选项*。由于您想实际存储更复杂的单个键值对,因此最好的选择是使用 sqlite 数据库。至于何时存储数据,我只会在每次使用OnCheckChangedListener单独检查或取消选中项目时触发存储。

*是的,还有其他选项,但这些是唯一易于使用的选项。

于 2011-11-09T03:07:38.197 回答