【发布时间】:2015-10-21 08:09:11
【问题描述】:
我想将我选择的 PhonesContacts(当前为 2 个字符串)添加到列表视图中。 目前我正在这样做:(姓名和电话号码被写入文本视图) 这是我想在列表中显示的字符串。
它已经在使用 textView,但是当我想添加第二个时,它会覆盖第一个。这就是为什么我想在列表中显示它。
如何将其更改为列表视图?
我尝试创建一个 ArrayList 并将字符串传递给该 ArrayList,但这没有奏效。
private void contactPicked(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null ;
String name = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
// Set the value to the textviews
textView1.setText(name);
textView2.setText(phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
}
对于我创建的列表视图
private ListView listView;
并在我的 OnCreate 中使用它
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_view);
listView = (ListView) findViewById(R.id.listView);
【问题讨论】:
-
有没有办法将字符串显示到列表视图中?
标签: android list listview contacts