Android 自动完成联系人并添加到ListView
我正在构建一个应用程序,我希望能够通过AutoCompleteTextView键入联系人的姓名,然后在选择联系人后,我希望能够将该联系人添加到ListView。到目前为止,我已经能够通过自动完成接收联系人姓名、电话号码和类型,代码如下:Android 自动完成联系人并添加到ListView,android,android-listview,autocomplete,Android,Android Listview,Autocomplete,我正在构建一个应用程序,我希望能够通过AutoCompleteTextView键入联系人的姓名,然后在选择联系人后,我希望能够将该联系人添加到ListView。到目前为止,我已经能够通过自动完成接收联系人姓名、电话号码和类型,代码如下: public class UserContactActivity extends Activity { private ArrayList<Map<String, String>> mPeopleList; priva
public class UserContactActivity extends Activity {
private ArrayList<Map<String, String>> mPeopleList;
private SimpleAdapter mAdapter;
private AutoCompleteTextView mTxtPhoneNo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_contacts);
mPeopleList = new ArrayList<Map<String, String>>();
PopulatePeopleList();
mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview ,new String[] { "Name", "Phone" , "Type" }, new int[] { R.id.ccontName, R.id.ccontNo, R.id.ccontType });
mTxtPhoneNo.setAdapter(mAdapter);
// This is the button that updates the user's list of emergency contacts
//Button btnSimple = (button) findViewById(R.id.btnSimple);
//btnSimple.setOnClickListener(new OnClickListener() {
//public void onClick(View v) (
// adds contact
//.notifyDataSetChanged();
}
//}
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();
如果这是一个简单的问题,很抱歉。我对android编程还不熟悉,但几天来一直在努力让它工作。非常感谢您的帮助。您可以使用BaseAdapter并传递自定义Listview单元格布局,而不是使用ArrayAdapter。或者将ArrayAdapter扩展到您自己的cudtom类。并膨胀你的布局 更多信息
谢谢你的回复,我会查看你发布的链接,看看是否能找到解决方案。我查看了这两个链接,但不幸的是我仍然迷路了。这些示例已经创建了字符串值。我正在寻找一种方法,使在自动完成文本视图中选择的联系人的姓名和电话号码显示为字符串值。我需要先将姓名和号码存储到数据库中吗?因此,从您的代码中,您可以将所有电话联系人存储在hashmap中,hashmap存储在arraylist中。您可以按用户从edittext serach在listview中搜索联系人,并在listview中显示联系人在hashmap中,但是我想我不知道如何在listview中存储或调用hashmap。我用于自动完成的代码非常相似,如果与此问题中的代码不同的话:。但是,我没有发布图片所需的声誉。
public class MainActivity extends Activity {
final ArrayList<String> phoneList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView = (ListView)findViewById(R.id.myListView);
final EditText myEditText = (EditText)findViewById(R.id.myEditText);
final ArrayList<String> phoneList = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa=new ArrayAdapter<String>(this, R.layout.custom_list_item, R.id.phoneName, phoneList);
myListView.setAdapter(aa);
Button btnSimple = (Button) findViewById(R.id.btnSimple);
btnSimple.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//adds contact
phoneList.add(0, myEditText.getText().toString());
//update the view
aa.notifyDataSetChanged();
//erases text to add phone
myEditText.setText("");
}
});
}
}