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

アプリケーションを通じてデータを共有する最善の方法は、すべてのデータを含むシングルトンを使用することだと思います。ここに良いチュートリアルがあります:

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

アプリの起動間で永続化が必要な場合は、SharedPreferences を使用することもできます。

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

于 2013-03-24T17:57:28.317 に答える