简体   繁体   English

将两个值添加到AutocompleteTextview适配器

[英]Add two values to AutocompleteTextview Adapter

I am trying to display contact list in autoCompleteTextView so far I have successfully achieved that but I need to use the contact_id related to name how Should I bind id with name? 到目前为止,我试图在autoCompleteTextView中显示联系人列表,但我已经成功实现了这一点,但是我需要使用与name相关的contact_id ,我应该如何将id与name绑定?

I have used hashmap to store the contact list. 我已经使用哈希图存储联系人列表。

here is my code to get contact and add them to hashmap 这是我的代码以获取联系并将其添加到hashmap

        Cursor cursor_number=getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
        if(cursor_number!=null){

            if (cursor_number.moveToFirst()){
                do{
                    contact_id=cursor_number.getString(cursor_number.getColumnIndex(ContactsContract.Data._ID));
                    if(Integer.parseInt(cursor_number.getString(cursor_number.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))>0) {
                        Cursor cursor_number1 = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.Data.CONTACT_ID + " = ? ", new String[]{contact_id}, null);
                        if (cursor_number1 != null) {
                            while (cursor_number1.moveToNext()) {
                                String name=cursor_number1.getString(cursor_number1.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                                String id=contact_id;
                                HashMap<String, String> contact_data=new HashMap<String, String>();
                                contact_data.put(id,name);
                            }
                            cursor_number1.close();
                        }
                    }

                }

                while (cursor_number.moveToNext());
                cursor_number.close();
            }
        }

and this is how i'm adding adapter to autoComplete textView 这就是我将适配器添加到autoComplete textView的方式

    autoCompleteTextView =(AutoCompleteTextView) this.getActivity().findViewById(R.id.act_network_auto_search);
    autoCompleteTextView.setThreshold(2);
    checkContacts();
    Collection<String> collection=contact_data.values();
    String[] array= collection.toArray(new String[collection.size()]);
    adapter = new ArrayAdapter<String>(getContext(),
            android.R.layout.simple_dropdown_item_1line,array);
    autoCompleteTextView.setAdapter(adapter);

How should I get the id associated with name 我应该如何获得与name相关的id

any help would be great! 任何帮助将是巨大的!

Update : 更新:

LinkedHashMap<String, String> contact_data=new LinkedHashMap<String, String>();
contact_data.put(id,name);

检查此示例AutocompleteTextView

you can get id from contact_data ,if you are sure the names are uniqe 您可以从contact_data获取ID,如果您确定名称是唯一的

public String getIdByName(String name) {
    for (Entry<String,String> entry : contact_data.entrySet()) {
        if (name.equals(value, entry.getValue())) {
            return entry.getKey();
        }
    }
    return null;
}

check this answer too 也检查这个答案

If it is compulsary for you to use key valu pair than I will sugest use LinkedHashMap insted of Hasmap because hashmap do not have ordering. 如果您必须使用键值对,那么我建议您使用Hasmap的LinkedHashMap,因为hashmap没有排序。

you can put data in linkedhasmap same as hashmap like follows 您可以将数据放入与哈希图相同的linkedhasmap中,如下所示

LinkedHashMap<String,String> lH = new LinkedHashMap<String,String>();
lH.put(id,name);

while in your auto complete text view do folowing 在自动完成文本视图中进行以下操作

autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View arg1, int pos,
            long id) {
            String key = (new ArrayList<String>(lH.keySet())).get(pos);
            you will have your id in key
       }
});

also you can make above efficient by just initializing lH.keySet out of listner also look at this http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html for LinkedHashMap 您也可以通过从列表器中初始化lH.keySet来提高效率,另外请参阅http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html中的LinkedHashMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM