0

一度に複数の連絡先を選択するために、電話のすべての連絡先を取得しようとしていlistviewます。checkboxesの連絡先の数と同じ数の行を作成するコードをいくつか書きましたemulator。しかし問題は、添付の画像でわかるように、連絡先の名前と番号が表示されないことです。さらに、[連絡先を表示] ボタンをクリックして選択した連絡先を表示しToastても、それは起こりません。

ここに画像の説明を入力

custcontactview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5.0px"
android:paddingLeft="5.0px"
android:paddingTop="5.0px" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="false" />

<TextView
    android:id="@+id/cConName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/checkBox1"
    android:layout_marginLeft="15.0dip"
    android:layout_toRightOf="@+id/checkBox1"
    android:text="Small Text"
    android:textAppearance="?android:textAppearanceSmall" />

<TextView
    android:id="@+id/cConNum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/cConName"
    android:layout_marginLeft="15.0dip"
    android:layout_toRightOf="@id/checkBox1"
    android:text="Medium Text"
    android:textAppearance="?android:textAppearanceMedium" />

</RelativeLayout>

activity_contacts_picker.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/btnShow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Selected" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/btnShow" >
</ListView>

</RelativeLayout>

ContactsPicker.java

public class ContactsPicker extends ListActivity {

protected Object mActionMode;
public int selectedItem = -1;
private Button btnShowContacts;
private ListView myListView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts_picker);
            myListView = getListView();
    btnShowContacts = (Button) findViewById(R.id.btnShow);
    btnShowContacts.setOnClickListener(new View.OnClickListener() {
        public void onClick (View v){
            Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
            String name = null;
            String number = null;
            long [] ids = myListView.getCheckedItemIds();
            for(long id : ids) {
                Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, null);
                while(contact.moveToNext()){
                    name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    number = contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                Toast.makeText(getApplicationContext(), "Name: " +name + "\n" + "Number: " + number , Toast.LENGTH_LONG).show();
            }
        }
    });
    ArrayList<Map<String, String>> list = buildData();
    String[] from = { "name", "purpose" };
    int[] to = { R.id.cConName, R.id.cConNum };
    SimpleAdapter adapter = new SimpleAdapter(this, list,R.layout.custcontactview, from, to);
    setListAdapter(adapter);
}

private ArrayList<Map<String, String>> buildData() {
    ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
    list.clear();
    Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null,null);
    while (people.moveToNext()) {
        String contactName = people.getString(people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        if ((Integer.parseInt(hasPhone) > 0)) {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
            while (phones.moveToNext()) {
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Map<String, String> NamePhoneType = new HashMap<String, String>();
                NamePhoneType.put("Name", contactName);
                NamePhoneType.put("Phone", phoneNumber);
                list.add(NamePhoneType);
            }
            phones.close();
        }
    }
    people.close();
    startManagingCursor(people);        
    return list;
  }
}
4

2 に答える 2

0

電話notifyDataSetChanged();リストを更新する必要があります。

于 2012-10-11T08:47:17.893 に答える
0

しかし問題は、添付の画像でわかるように、連絡先の名前と番号が表示されないことです

まずArrayList listbuildDataメソッド内に実際の値があることを確認したと思います。次に、 に値を追加するときにキー名"Name"とを使用しますが、 で使用する文字列配列では、との値を使用します。String 配列には、 と同じキーが必要です。"Phone"HashMapfromSimpleAdapter"name""purpose"fromHashMap

さらに、[連絡先を表示] ボタンをクリックして選択した連絡先を Toast に表示しても、それは行われません。

まず、カスタム行ビューを使用している場合、チェックされた項目に対して ListView の既定のメカニズムを使用することはできません。チェックされたアイテムを取得したい場合は、CheckBox自分で行からの状態を管理する必要があります。メソッドはgetCheckedItemIds()有効なものを返さず、クエリは有効な結果を返すことに失敗します。代わりに、独自のカスタム アダプターを実装し、メソッドを使用して行HashMap(名前と電話番号を含む) を返す必要がありますSimpleAdapter.getItem。これにより、連絡先を再度クエリする必要がなくなります。

于 2012-10-11T08:21:38.050 に答える