Android MultiAutoCompleteTextView用于检索MultiAutoCompleteTextView的多个联系人

时间:2013-10-24 07:05:26

标签: android autocomplete contacts

我很抱歉,如果这看起来像很多次同样的问题......但谷歌搜索这个没有提供任何结果  这可能与link相同,但无法获得解决方案

我正在尝试修改我在stackoverflow上看到的AutoCompleteTextView代码,以便我将返回MultiAutoCompleteTextView的多个联系人。 当我在洗脱器上执行时,我再也看不到任何可供选择的建议。我感觉到了这个问题。

提前感谢您的帮助。

我的multiautocompletetextview有以下代码:

<MultiAutoCompleteTextView
     android:id="@+id/mmWhoNo"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textColor="#0000A0"
     android:hint="Choose your Contacts" />

最后,我有以下内容阅读我从autocomplete textview

选择联系人时所采取的联系方式
and my activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/ccontName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#A5000000" />

<TextView
    android:id="@+id/ccontNo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/ccontName"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#A5000000" />

<TextView
    android:id="@+id/ccontType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/ccontNo"
    android:layout_alignParentRight="true"
    android:layout_marginRight="14dp"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#A5000000" />
 </LinearLayout>


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
    private ArrayList<Map<String, String>> mPeopleList;
    private SimpleAdapter mAdapter;
    private MultiAutoCompleteTextView mTxtPhoneNo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_with_auto);
        mPeopleList = new ArrayList<Map<String, String>>();
        PopulatePeopleList();
        mTxtPhoneNo = (MultiAutoCompleteTextView) findViewById(R.id.mmWhoNo);
        mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.activity_main,
                new String[] { "Name", "Phone", "Type" }, new int[] {
                        R.id.ccontName, R.id.ccontNo, R.id.ccontType });
        mTxtPhoneNo.setAdapter(mAdapter);

    }

    public void PopulatePeopleList() {
        mPeopleList.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)){
                // You know have the number so now query it like this
                Cursor phones = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
                null, null);
                while (phones.moveToNext()){
                    //store numbers and display a dialog letting the user select which.
                    String phoneNumber = phones.getString(
                    phones.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String numberType = phones.getString(phones.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.TYPE));
                    Map<String, String> NamePhoneType = new HashMap<String, String>();
                    NamePhoneType.put("Name", contactName);
                    NamePhoneType.put("Phone", phoneNumber);
                    if(numberType.equals("0"))
                        NamePhoneType.put("Type", "Work");
                        else
                        if(numberType.equals("1"))
                        NamePhoneType.put("Type", "Home");
                        else if(numberType.equals("2"))
                        NamePhoneType.put("Type",  "Mobile");
                        else
                        NamePhoneType.put("Type", "Other");
                        //Then add this map to the list.
                        mPeopleList.add(NamePhoneType);
                }
                phones.close();
            }
        }
        people.close();
        startManagingCursor(people);
    }

    public void onItemClick(AdapterView<?> av, View v, int index, long arg){
//      Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
//      Iterator<String> myVeryOwnIterator = map.keySet().iterator();
//      while(myVeryOwnIterator.hasNext()) {
//          String key=(String)myVeryOwnIterator.next();
//          String value=(String)map.get(key);
//          mTxtPhoneNo.setText(value);
//      }

                Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);

                String name  = map.get("Name");
                String number = map.get("Phone");
                mTxtPhoneNo.setText(""+name+"<"+number+">");



    }


}

任何机构都可以建议我获取多个联系方式。在MultiAutoCompleteTextView

we have to change in manifast.xml


<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS" />

0 个答案:

没有答案