我是Android新手,点击按钮时如何从MultiAutocompleteTextview获取所选联系人的电话号码? 读取多个自动完成文本视图的联系人的方法
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);
}
}
答案 0 :(得分:1)
我通过更改下面的代码解决了多自动完成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中,同时按钮单击将所选联系人拆分为“,”并迭代每个名称的循环以获取联系号码。
通过这种方式我解决了我的问题希望它有用!!如果是这样投票答案!!!