【发布时间】:2014-02-07 11:35:30
【问题描述】:
我是 android 技术的初学者。我有一个列表视图,其中显示了我的联系人姓名,我希望当我单击列表视图项目时,特定联系人的联系人 ID 显示在 toast 中。我尝试了很多但无法做到这一点。也没有在谷歌上找到合适的例子
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listcontact = (ListView)findViewById(R.id.listView1);
getnumber(this.getContentResolver());
listcontact.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
/* ContentResolver cr2 = null;
Cursor phone2 = cr2.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
String uid=phone2.getString(phone2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));*/
String getselected=(String)(listcontact.getItemAtPosition(arg2));
long getsele=(long)(listcontact.getItemIdAtPosition(arg2));
String s=String.valueOf(getsele);
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
//phoneCall(getselected);
//Intent callIntent = new Intent(Intent.ACTION_CALL);
//callIntent.setData(Uri.parse("tel:0377778888"));
//startActivity(callIntent);
}
});
edittext=(EditText)findViewById(R.id.editText1);
edittext.requestFocus();
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
MainActivity.this.adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void getnumber(ContentResolver cr) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phone.moveToNext()) {
phonenumber=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
System.out.println("...........name");
String list=name+
" "+phonenumber;
aa.add(list);
}
phone.close();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,aa);
listcontact.setAdapter(adapter);
}
提前致谢。
【问题讨论】:
-
String id =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
-
Mayuri 我想要 itemclick 中的联系人 ID
标签: android eclipse listview android-listview