字符串的ListView的Android PhoneContacts [英] String to ListView Android PhoneContacts
问题描述
我想我的选择PhonesContacts(当前为2串)添加到列表视图。
目前即时通讯这样:(姓名和PHONENO被写入的TextView)
这是我想在列表中显示的字符串。
及其与TextView的工作已经但当我想添加第二个它覆盖的第一个。这就是为什么我想它显示在列表中。
我怎样才能将其更改为ListView的?
我试图创造一个ArrayList和传递字符串此ArrayList,但这一直没有工作。
私人无效contactPicked(意向数据){
光标光标= NULL;
尝试{
字符串PHONENO = NULL;
字符串名称= NULL;
// getData()方法将选定联系人的内容乌里
URI URI = data.getData();
//查询内容URI
光标= getContentResolver()查询(URI,NULL,NULL,NULL,NULL);
cursor.moveToFirst();
//电话号码的列索引
INT phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
//联系人姓名的列索引
INT nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
PHONENO = cursor.getString(phoneIndex);
名称= cursor.getString(nameIndex);
//将值设置为textviews
textView1.setText(名);
textView2.setText(PHONENO); }赶上(例外五){
e.printStackTrace();
}
}
对于ListView我创建了一个
私人的ListView ListView的;
,并用它在我的OnCreate
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_contact_view);
ListView控件=(ListView控件)findViewById(R.id.listView);
私人无效contactPicked(){
光标光标= NULL;
ArrayList的<串GT; phonenumberList =新的ArrayList<串GT;(); //此处声明的ArrayList
ArrayList的<串GT; NAMELIST =新的ArrayList<串GT;(); //此处声明的ArrayList
尝试{
字符串PHONENO = NULL;
字符串名称= NULL;
// getData()方法将选定联系人的内容乌里 光标= getContentResolver()查询(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);
cursor.moveToFirst();
//电话号码的列索引
INT phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
//联系人姓名的列索引
INT nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
PHONENO = cursor.getString(phoneIndex);
名称= cursor.getString(nameIndex);
//将值设置为textviews
textView1.setText(名);
textView2.setText(PHONENO); phonenumberList.add(PHONENO); //数组列表中增加价值
nameList.add(名); //数组列表中增加价值
}赶上(例外五){
e.printStackTrace();
}
}
在列表视图设置适配器:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_contact_view);
ListView控件=(ListView控件)findViewById(R.id.listView); contactPicked(); ArrayAdapter<串GT; arrayAdapter =
新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,名称列表); //这里设置具有两个TextView的自定义适配器。
//设置适配器
listView.setAdapter(arrayAdapter);
如何创建自定义适配器,见下面的链接:
I'd like to add my selected PhonesContacts(currently 2 strings) to a listview. Currently im doing this: (Name and PhoneNo are written to the textview) This are the strings that i'd like to display in a list.
Its working with textView already but when i want to add a second one it overwrites the first one. Thats why i'd like to show it in a list.
How can i change this to listview?
I tried creating an ArrayList and passing the string to this ArrayList but this hasn't been working.
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();
}
}
For the listview i created a
private ListView listView;
and used it in my OnCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_view);
listView = (ListView) findViewById(R.id.listView);
private void contactPicked() {
Cursor cursor = null;
ArrayList<String> phonenumberList = new ArrayList<String>(); // Declare ArrayList here
ArrayList<String> nameList = new ArrayList<String>(); // Declare ArrayList here
try {
String phoneNo = null ;
String name = null;
// getData() method will have the Content Uri of the selected contact
cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_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);
phonenumberList.add(phoneNo); // add value in arraylist
nameList.add(name); // add value in arraylist
} catch (Exception e) {
e.printStackTrace();
}
}
Set adapter in listview :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_view);
listView = (ListView) findViewById(R.id.listView);
contactPicked();
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, nameList); // Here Set your custom adapter which have two textview .
// Set The Adapter
listView.setAdapter(arrayAdapter);
How to create custom adapter see below link :
这篇关于字符串的ListView的Android PhoneContacts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!