在两行 ListView 中列出联系人信息
list contact information in two line ListView
我想在两行中列出联系人详细信息(人名,人号)ListView
(第一行:人名,第二行:phone 号码) .我成功地获得了联系人的所有必要信息,但是我在 ListView
中列出他们时遇到了问题。 ListView
中仅显示了第一个联系人。
在 onCreate()
我创建了 ContentResolver
对象,调用方法 get_list( )
获取名称和 phone 号码并尝试显示 ListView
:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int anzahl = cur.getCount();
Log.d("anzahl09", "anzahl09 " + anzahl);
telefonname = new String[anzahl];
telefonnummer = new String[anzahl];
zusammengesetzt = new String[anzahl];
kontakte_laden = new String[anzahl][anzahl];
String[] telefonname_telefonnummer = new String[anzahl];
telefonname_telefonnummer = get_list(cur, cr);
int i =0;
do {
Log.d("schleife09", "schleife09 " + i + "|" + telefonname_telefonnummer[i] + "|" + telefonname[i] + "|" + telefonnummer[i] + "|" + kontakte_laden[i][i]);
kontakte_laden[i][0] = telefonname[i];
kontakte_laden[i][1] = telefonnummer[i];
i++;
} while (i < anzahl);
ListView listview = ( ListView ) findViewById ( android.R.id.list );
listview.setPadding ( 20, 20, 20, 20 );
@SuppressWarnings("Convert2Diamond") ArrayList< HashMap< String, String > > list = new ArrayList< HashMap< String, String > > ();
for (String[] anAussehen : kontakte_laden) {
//noinspection Convert2Diamond
item = new HashMap<String, String>();
item.put("line1", anAussehen[0]);
item.put("line2", anAussehen[1]);
list.add(item);
}
SimpleAdapter sa = new SimpleAdapter(this, list,
android.R.layout.two_line_list_item,
new String[]{"line1", "line2"},
new int[]{android.R.id.text1, android.R.id.text2});
listview.setAdapter(sa);
listview.setOnItemClickListener(listViewOnItemClickListener);
读取联系信息的方法 get_list()
如下所示:
private String[] get_list(Cursor cur, ContentResolver cr){
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
int i=0;
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
telefonname[i] = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d("telefonname09", "telefonname09 " +telefonname[i]);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
telefonnummer[i] = pCur.getString( pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER ) );
Log.d("telefonnummer09", "telefonnummer09 " + telefonnummer[i]); // correct values
}
pCur.close();
}
}
zusammengesetzt[i] = telefonname[i] + "|" + telefonnummer[i];
}
}
// only one value
for(int i=0; i < cur.getCount(); i++){
Log.d("telefonnummer07", "telefonnummer07 " + telefonname[i] + "|" + telefonnummer[i]);
}
cur.close();
return zusammengesetzt;
}
但结果如下:
更新
问题是,人名 (telefonname[]
) 和 phone 号码 (telefonnummer[]
) 的数组只在 [=21] 的 while-loop 中=] 方法正确的内容。在 while-loop 之外,数组只有最后一个值。参见上面的方法get_list()
。
解决方案
我更改了以下几点:
- 我之前直接给全局String数组定义了编号和名称。所以在
get_list()
方法中我必须定义两个新的字符串数组
- 我将名称和 phone 编号引用到这些新的本地字符串数组
- 将这些新的本地字符串数组引用到全局字符串数组
这是工作代码:
onCreate()
方法:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int anzahl = cur.getCount();
Log.d("anzahl09", "anzahl09 " + anzahl);
telefonname_global = new String[anzahl];
telefonnummer_global = new String[anzahl];
kontakte_laden = new String[anzahl][anzahl];
get_list(cur, cr);
int i =0;
do {
Log.d("schleife09", "schleife09 " + i + "|" /*+ telefonname_telefonnummer[i] + "|" */ + telefonname_global[i] + "|" + telefonnummer_global[i] + "|" + kontakte_laden[i][i]);
kontakte_laden[i][0] = telefonname_global[i];
kontakte_laden[i][1] = telefonnummer_global[i];
i++;
} while (i < anzahl);
ListView listview = ( ListView ) findViewById ( android.R.id.list );
listview.setPadding ( 20, 20, 20, 20 );
@SuppressWarnings("Convert2Diamond") ArrayList< HashMap< String, String > > list = new ArrayList< HashMap< String, String > > ();
for (String[] anAussehen : kontakte_laden) {
//noinspection Convert2Diamond
item = new HashMap<String, String>();
item.put("line1", anAussehen[0]);
item.put("line2", anAussehen[1]);
list.add(item);
}
SimpleAdapter sa = new SimpleAdapter(this, list,
android.R.layout.two_line_list_item,
new String[]{"line1", "line2"},
new int[]{android.R.id.text1, android.R.id.text2});
listview.setAdapter(sa);
listview.setOnItemClickListener(listViewOnItemClickListener);
和 get_list()
方法:
private void get_list(Cursor cur, ContentResolver cr){
int i = 0;
if (cur.getCount() > 0) {
String[] telefonname = new String[cur.getCount()];
String[] telefonnummer = new String[cur.getCount()];
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
telefonname[i] = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d("telefonname09", "telefonname09 " +telefonname[i]);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
telefonnummer[i] = pCur.getString( pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER ) );
Log.d("telefonnummer09", "telefonnummer09 " + telefonnummer[i]);
}
pCur.close();
}
}
Log.d("telefonnummer08", "telefonnummer08 " + telefonnummer[i]);
telefonname_global[i] = telefonname[i];
telefonnummer_global[i] = telefonnummer[i];
i++;
}
}
// Fehler: Array telefonnummer hat nur einen Wert
for(int j=0; j < cur.getCount(); j++){
Log.d("telefonnummer07", "telefonnummer07 " + telefonname_global[j] + "|" + telefonnummer_global[j]);
}
cur.close();
}
非常感谢您的帮助。
另一个问题是,没有 phone 号码的 e-mail 地址或联系人不应该在此列表中。如何做到这一点?
i 不是增量那是问题所在
private String[] get_list(Cursor cur, ContentResolver cr){
if (cur.getCount() > 0) {
// inialize outside
int i=0;
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
telefonname[i] = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d("telefonname09", "telefonname09 " +telefonname[i]);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
telefonnummer[i] = pCur.getString( pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER ) );
Log.d("telefonnummer09", "telefonnummer09 " + telefonnummer[i]); // correct values
}
pCur.close();
}
}
zusammengesetzt[i] = telefonname[i] + "|" + telefonnummer[i];
//i is need to increment
i++;
}
}
仅当 phone 个号码存在时添加
for (String[] anAussehen : kontakte_laden) {
if(anAussehen[1] != null)
{
//noinspection Convert2Diamond
item = new HashMap<String, String>();
item.put("line1", anAussehen[0]);
item.put("line2", anAussehen[1]);
list.add(item);
}
}
我已经结束了对从 Android Phone 检索的最快解决方案的搜索。 ContactDTO 是 pojo class.
它将return导致"Contact Display Name"的升序。
public static List<ContactDTO> getPhone(Context context){
List<ContactDTO> contactList = new ArrayList<ContactDTO>();
ContentResolver cr = context.getContentResolver();
String[] PROJECTION = new String[] {
ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + Phone.TYPE +"=" + Phone.TYPE_MOBILE;
String order = ContactsContract.Contacts.DISPLAY_NAME + " ASC";// LIMIT " + limit + " offset " + lastId + "";
Cursor phoneCur = cr.query(uri, PROJECTION, filter, null, order);
while(phoneCur.moveToNext()){
ContactDTO dto = new ContactDTO();
dto.setName("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
dto.setMobileNo("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
dto.setPhotoUrl("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)));
dto.setContactId("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID)));
contactList.add(dto);
}
phoneCur.close();
return contactList;
}
我想在两行中列出联系人详细信息(人名,人号)ListView
(第一行:人名,第二行:phone 号码) .我成功地获得了联系人的所有必要信息,但是我在 ListView
中列出他们时遇到了问题。 ListView
中仅显示了第一个联系人。
在 onCreate()
我创建了 ContentResolver
对象,调用方法 get_list( )
获取名称和 phone 号码并尝试显示 ListView
:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int anzahl = cur.getCount();
Log.d("anzahl09", "anzahl09 " + anzahl);
telefonname = new String[anzahl];
telefonnummer = new String[anzahl];
zusammengesetzt = new String[anzahl];
kontakte_laden = new String[anzahl][anzahl];
String[] telefonname_telefonnummer = new String[anzahl];
telefonname_telefonnummer = get_list(cur, cr);
int i =0;
do {
Log.d("schleife09", "schleife09 " + i + "|" + telefonname_telefonnummer[i] + "|" + telefonname[i] + "|" + telefonnummer[i] + "|" + kontakte_laden[i][i]);
kontakte_laden[i][0] = telefonname[i];
kontakte_laden[i][1] = telefonnummer[i];
i++;
} while (i < anzahl);
ListView listview = ( ListView ) findViewById ( android.R.id.list );
listview.setPadding ( 20, 20, 20, 20 );
@SuppressWarnings("Convert2Diamond") ArrayList< HashMap< String, String > > list = new ArrayList< HashMap< String, String > > ();
for (String[] anAussehen : kontakte_laden) {
//noinspection Convert2Diamond
item = new HashMap<String, String>();
item.put("line1", anAussehen[0]);
item.put("line2", anAussehen[1]);
list.add(item);
}
SimpleAdapter sa = new SimpleAdapter(this, list,
android.R.layout.two_line_list_item,
new String[]{"line1", "line2"},
new int[]{android.R.id.text1, android.R.id.text2});
listview.setAdapter(sa);
listview.setOnItemClickListener(listViewOnItemClickListener);
读取联系信息的方法 get_list()
如下所示:
private String[] get_list(Cursor cur, ContentResolver cr){
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
int i=0;
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
telefonname[i] = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d("telefonname09", "telefonname09 " +telefonname[i]);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
telefonnummer[i] = pCur.getString( pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER ) );
Log.d("telefonnummer09", "telefonnummer09 " + telefonnummer[i]); // correct values
}
pCur.close();
}
}
zusammengesetzt[i] = telefonname[i] + "|" + telefonnummer[i];
}
}
// only one value
for(int i=0; i < cur.getCount(); i++){
Log.d("telefonnummer07", "telefonnummer07 " + telefonname[i] + "|" + telefonnummer[i]);
}
cur.close();
return zusammengesetzt;
}
但结果如下:
更新
问题是,人名 (telefonname[]
) 和 phone 号码 (telefonnummer[]
) 的数组只在 [=21] 的 while-loop 中=] 方法正确的内容。在 while-loop 之外,数组只有最后一个值。参见上面的方法get_list()
。
解决方案 我更改了以下几点:
- 我之前直接给全局String数组定义了编号和名称。所以在
get_list()
方法中我必须定义两个新的字符串数组 - 我将名称和 phone 编号引用到这些新的本地字符串数组
- 将这些新的本地字符串数组引用到全局字符串数组
这是工作代码:
onCreate()
方法:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int anzahl = cur.getCount();
Log.d("anzahl09", "anzahl09 " + anzahl);
telefonname_global = new String[anzahl];
telefonnummer_global = new String[anzahl];
kontakte_laden = new String[anzahl][anzahl];
get_list(cur, cr);
int i =0;
do {
Log.d("schleife09", "schleife09 " + i + "|" /*+ telefonname_telefonnummer[i] + "|" */ + telefonname_global[i] + "|" + telefonnummer_global[i] + "|" + kontakte_laden[i][i]);
kontakte_laden[i][0] = telefonname_global[i];
kontakte_laden[i][1] = telefonnummer_global[i];
i++;
} while (i < anzahl);
ListView listview = ( ListView ) findViewById ( android.R.id.list );
listview.setPadding ( 20, 20, 20, 20 );
@SuppressWarnings("Convert2Diamond") ArrayList< HashMap< String, String > > list = new ArrayList< HashMap< String, String > > ();
for (String[] anAussehen : kontakte_laden) {
//noinspection Convert2Diamond
item = new HashMap<String, String>();
item.put("line1", anAussehen[0]);
item.put("line2", anAussehen[1]);
list.add(item);
}
SimpleAdapter sa = new SimpleAdapter(this, list,
android.R.layout.two_line_list_item,
new String[]{"line1", "line2"},
new int[]{android.R.id.text1, android.R.id.text2});
listview.setAdapter(sa);
listview.setOnItemClickListener(listViewOnItemClickListener);
和 get_list()
方法:
private void get_list(Cursor cur, ContentResolver cr){
int i = 0;
if (cur.getCount() > 0) {
String[] telefonname = new String[cur.getCount()];
String[] telefonnummer = new String[cur.getCount()];
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
telefonname[i] = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d("telefonname09", "telefonname09 " +telefonname[i]);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
telefonnummer[i] = pCur.getString( pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER ) );
Log.d("telefonnummer09", "telefonnummer09 " + telefonnummer[i]);
}
pCur.close();
}
}
Log.d("telefonnummer08", "telefonnummer08 " + telefonnummer[i]);
telefonname_global[i] = telefonname[i];
telefonnummer_global[i] = telefonnummer[i];
i++;
}
}
// Fehler: Array telefonnummer hat nur einen Wert
for(int j=0; j < cur.getCount(); j++){
Log.d("telefonnummer07", "telefonnummer07 " + telefonname_global[j] + "|" + telefonnummer_global[j]);
}
cur.close();
}
非常感谢您的帮助。
另一个问题是,没有 phone 号码的 e-mail 地址或联系人不应该在此列表中。如何做到这一点?
i 不是增量那是问题所在
private String[] get_list(Cursor cur, ContentResolver cr){
if (cur.getCount() > 0) {
// inialize outside
int i=0;
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
telefonname[i] = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d("telefonname09", "telefonname09 " +telefonname[i]);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
telefonnummer[i] = pCur.getString( pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER ) );
Log.d("telefonnummer09", "telefonnummer09 " + telefonnummer[i]); // correct values
}
pCur.close();
}
}
zusammengesetzt[i] = telefonname[i] + "|" + telefonnummer[i];
//i is need to increment
i++;
}
}
仅当 phone 个号码存在时添加
for (String[] anAussehen : kontakte_laden) {
if(anAussehen[1] != null)
{
//noinspection Convert2Diamond
item = new HashMap<String, String>();
item.put("line1", anAussehen[0]);
item.put("line2", anAussehen[1]);
list.add(item);
}
}
我已经结束了对从 Android Phone 检索的最快解决方案的搜索。 ContactDTO 是 pojo class.
它将return导致"Contact Display Name"的升序。
public static List<ContactDTO> getPhone(Context context){
List<ContactDTO> contactList = new ArrayList<ContactDTO>();
ContentResolver cr = context.getContentResolver();
String[] PROJECTION = new String[] {
ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + Phone.TYPE +"=" + Phone.TYPE_MOBILE;
String order = ContactsContract.Contacts.DISPLAY_NAME + " ASC";// LIMIT " + limit + " offset " + lastId + "";
Cursor phoneCur = cr.query(uri, PROJECTION, filter, null, order);
while(phoneCur.moveToNext()){
ContactDTO dto = new ContactDTO();
dto.setName("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
dto.setMobileNo("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
dto.setPhotoUrl("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)));
dto.setContactId("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID)));
contactList.add(dto);
}
phoneCur.close();
return contactList;
}