按列表项后,无法获取列表项的信息
到目前为止,我已经研究了许多溢出问题,并且我不知道我是否正确设置了列表视图,但是我不想弄乱我如何能够获取联系人
public class ViewContacts extends AppCompatActivity {
private ListView lv;
private Cursor cursor1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_contacts);
cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor1);
String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};
int[] to = {android.R.id.text1, android.R.id.text2};
final SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor1, from, to);
ListView lv = findViewById(R.id.list);
lv.setAdapter(listAdapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder option = new AlertDialog.Builder(ViewContacts.this);
option.setMessage(the phone number);
option.show();
}
});
}
}
按列表项,然后弹出显示电话号码
答案 0 :(得分:1)
如果要使用所点击的联系人项目的电话号码,则需要使用position
将光标移至相应的行,然后从所需的项目中获取该项目。列。
下面放在onItemClick
中的简化示例:
cursor1.moveToPosition(position);
String phoneNumber = cursor1.getString(cursor1.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
您可以将ContactsContract.CommonDataKinds.Phone.NUMBER
替换为任何有效的列名。