繁体   English   中英

从多个自动完成文本视图中获取选定的联系人电话号码

[英]get the selected contact phone numbers from multi autocomplete textview

我是Android新手,单击按钮时如何从MultiAutocompleteTextview获取选定联系人的电话号码? 读取联系人以实现多自动完成textview的方法

private void readContactData() {
    // TODO Auto-generated method stub

    String phoneNumber = "";
    String phoneName = "";
    phoneValueArr.clear();
    nameValueArr.clear();

    try{

    ContentResolver content = getContentResolver();

    cursor = content.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            PEOPLE_PROJECTION, null, null, null);

    if(null != cursor && cursor.moveToFirst()){
        do{
            // Get Phone number
            phoneNumber =""+cursor.getString(cursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phoneName = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            phoneValueArr.add(phoneNumber.toString());
            nameValueArr.add(phoneName.toString());
        }while(cursor.moveToNext());

    }

    //cursor.close();

    }catch(Exception e){

        Log.i("AutocompleteContacts","Exception : "+ e);

    }finally {
        //if (null != cursor)
            //cursor.close();
    }

    ContactListAdapter adapter = new ContactListAdapter(this, cursor);
    mAuto.setAdapter(adapter);
}

我的ContactsListAdapter

public static class ContactListAdapter extends CursorAdapter implements Filterable {
    public ContactListAdapter(Context context, Cursor c) {
        super(context, c);
        mContent = context.getContentResolver();
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
         final LayoutInflater inflater = LayoutInflater.from(context);

         View retView = inflater.inflate(R.layout.schedule_msg_custcontview,parent,false);
         return retView;

    }        

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        //((TextView) view).setText(cursor.getString(2));
        TextView pname = (TextView)view.findViewById(R.id.ccontName);
        TextView pnum = (TextView)view.findViewById(R.id.ccontNo); 
        pname.setText(cursor.getString(2));
        pnum.setText(cursor.getString(1));

    }

    @Override
    public String convertToString(Cursor cursor) {
        return cursor.getString(2);
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (getFilterQueryProvider() != null) {
            return getFilterQueryProvider().runQuery(constraint);
        }

        StringBuilder buffer = null;
        String[] args = null;
        if (constraint != null) {
            buffer = new StringBuilder();
        buffer.append("UPPER(");
        buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
        buffer.append(") GLOB ?");
            args = new String[] { constraint.toString().toUpperCase() + "*" };
        }

        return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PEOPLE_PROJECTION,
                buffer == null ? null : buffer.toString(), args,
                null);
    }



    private ContentResolver mContent;           
}

private static final String[] PEOPLE_PROJECTION = new String[] {
    ContactsContract.Contacts._ID,
    ContactsContract.CommonDataKinds.Phone.NUMBER,
    ContactsContract.Contacts.DISPLAY_NAME,
};

以及如何在按下按钮的同时将所选的联系人号码放入对象以存储在数据库中。 并且在加载联系人时给出了一个例外,

12-11 12:39:11.422: I/AutocompleteContacts(17735): Exception :   android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 188

谁能帮我 ?

这是我的自动完成的OnItemClick侦听器,它始终为选定名称提供索引索引-1

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
// Get Array index value for selected name
int i = nameValueArr.indexOf(""+parent.getItemAtPosition(position));

// If name exist in name ArrayList
if (i >= 0) {

// Get Phone Number
toNumberValue = phoneValueArr.get(i);

InputMethodManager imm = (InputMethodManager) getSystemService(
        INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

   // Show Alert        
Toast.makeText(getBaseContext(), "Position:"+position+" Name:"+parent.getItemAtPosition(position)+" Number:"+toNumberValue,
            Toast.LENGTH_LONG).show();

Log.d("AutocompleteContacts", "Position:"+position+" Name:"+parent.getItemAtPosition(position)+" Number:"+toNumberValue);

}
}

我通过更改以下代码解决了多自动完成OnItemclickListener问题

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub

TextView temp = (TextView) view.findViewById(R.id.ccontNo);
TextView temp2 = (TextView) view.findViewById(R.id.ccontName);
final String selectedNumber = temp.getText().toString();
final String selectedName = temp2.getText().toString();

if (selectedNumber != null) {


InputMethodManager imm = (InputMethodManager) getSystemService(
        INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

   // Show Alert        
Toast.makeText(getBaseContext(), " Name:"+selectedName+" Number:"+selectedNumber, Toast.LENGTH_LONG).show();

}
}

现在,我正在获取选定的联系人姓名和电话号码。 然后将这些值存储在HashMap中,同时单击按钮时,将选定的联系人用“,”分隔,并循环每个名称的循环以获取联系人号码。

这样我解决了我的问题,希望对您有帮助! 如果是这样,投票给答案!!!

暂无
暂无

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

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