0

使用以下代码,我正在使用联系人姓名和相应的复选框填充列表视图。

目前,代码只是将整个列表吐到日志中。

我打算从列表中获取选定的联系人并将它们存储在本地,以便它们可以在应用程序的其他活动中使用。

请问您能提供一些指导吗?

我查看了 android 文档以及这里的许多帖子。我现在在这件事上寻求你的帮助。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.nominatecontactsactivitytest);

    this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    startManagingCursor(cursor);
    String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME};

    int[] to = new int[] { android.R.id.text1 };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, cursor, columns, to);
    this.setListAdapter(adapter);

    Button finishButton = (Button) this.findViewById(R.id.finishButton);
    finishButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            SimpleCursorAdapter adapter = (SimpleCursorAdapter) nominateContactsActivity.this.getListAdapter();
            Cursor cursor = adapter.getCursor();

            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            ListView lv = nominateContactsActivity.this.getListView();
            SparseBooleanArray selectedItems = lv.getCheckedItemPositions();

            for (int i = 0; i < selectedItems.size(); i++) {
                int selectedPosition = selectedItems.keyAt(i);
                cursor.moveToPosition(selectedPosition);

                strName=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                strTelNo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                                                      
                Log.d("NAME: ",strName);
                Log.d("NUM: ", strTelNo);

                }cursor.close();
            }
        });
    }

非常感谢。

编辑:

好的,所以我将使用 sharedpreferences 来存储一个序列化的联系人数组。

不过,我仍然面临实际生成这个数组的问题——有人可以帮忙吗?

4

1 回答 1

0

I think the best way to share data through your application is to use a Singleton which contain all your data. Here is a good tutorial:

http://www.roseindia.net/java/beginners/SingletonPattern.shtml

You can also use SharedPreferences if you want persistance between launches of your app:

http://developer.android.com/reference/android/content/SharedPreferences.html

于 2013-03-24T17:57:28.317 回答