我正在构建一个应用程序,我希望能够通过AutoCompleteTextView
输入联系人的姓名,然后在选择联系人后,我希望能够将该联系人添加到ListView
。到目前为止,我已经能够通过以下代码通过自动完成功能接收联系人姓名,电话号码和类型:
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();
现在我希望能够将所选联系人添加到列表视图中,只显示他们的姓名和号码。这是我当前的java
文件,其中包含arrayadapter
和list
:
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("");
}
});
}
}
很抱歉,这是一个简单的问题。我是Android的编程新手,但一直试图让它工作好几天。非常感谢任何帮助,谢谢你们。
答案 0 :(得分:0)
而不是使用ArrayAdapter,您可以使用BaseAdapter并传递自定义Listview单元格布局。或者将ArrayAdapter扩展到您自己的cudtom类。并夸大你的布局。
了解更多信息
http://developer.android.com/reference/android/widget/ArrayAdapter.html
http://www.vogella.com/tutorials/AndroidListView/article.html