列出两行ListView

时间:2015-09-19 09:30:22

标签: android listview android-contacts android-cursor android-contentresolver

我想在两行ListView中列出联系方式(人名,人数)(第一行:人名,第二行:电话号码)。我成功地获得了联系人的所有必要信息,但是我在ListView中列出它们时遇到了问题。只有第一次联系才会显示在ListView中。

<{1>} onCreate()我创建ContentResolver对象,调用方法get_list( )获取姓名和电话号码并尝试显示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;
}

但结果如下: enter image description here

更新 问题是,人员姓名(telefonname[])和电话号码(telefonnummer[])的数组只能在get_list()方法的while循环中获得正确的内容。在while循环之外,数组只有最后一个值。请参阅上面的方法get_list()

我改变了以下几点:

  • 我之前直接定义了全局String数组的编号和名称。所以在get_list()方法中我必须定义两个新的String数组
  • 我将名称和电话号码引用到这些新的本地字符串数组
  • 将这些新的本地String数组引用到全局String数组

这是工作代码:

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();

}

非常感谢您的帮助。

另一个问题是,没有电话号码的电子邮件地址或联系人不应该在此列表中。怎么做到这一点?

2 个答案:

答案 0 :(得分:2)

我不是增量问题

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++;
    }
}

仅在存在电话号码时添加

 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);
  }
}

答案 1 :(得分:0)

我已经找到了从Android手机中检索最快的解决方案。 ContactDTO是pojo课程。

它将按照&#34;联系人显示名称&#34;。

的升序返回结果
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;
}