在listView中读取联系人的优化?

时间:2012-02-04 19:05:16

标签: java android

我有代码来读取联系人,获取姓名和号码并将它们放在listView中。一切正常,但应用程序在同一时刻读取所有联系人。我想我必须在滚动动作中阅读它们,但我现在不怎么样,请帮忙吗? 沃尔夫,提前谢谢你。

这是我的代码:

的ArrayList> mapa = new ArrayList>();

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);


        if(cur.getCount() > 0){
            while(cur.moveToNext()){
                id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){

                    final Cursor numCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "  = ?", new String[]{id}, null);

                    for(numCur.moveToFirst(); !numCur.isAfterLast(); numCur.moveToNext()){

                        brTel = numCur.getString(numCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        ime = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        tmpIme = new String[] {ime};

                        for(int i = 0; i < tmpIme.length; i++){

                            HashMap<String, String> imeMapa = new HashMap<String, String>();
                            imeMapa.put("imeLista", ime);
                            imeMapa.put("Mobilni", brTel);
                            mapa.add(imeMapa);

                        }

                    }
                    numCur.close();

                }

            } // While
        }

        SimpleAdapter sa = new SimpleAdapter(getApplicationContext(), mapa, R.layout.imenik, new String[] {"imeLista", "Mobilni"}, new int[] {R.id.tvImeImenik, R.id.tvSamoProba});
        lImenik.setAdapter(sa);

1 个答案:

答案 0 :(得分:0)

我认为你应该使用SimpleCursorAdapter,之后应用程序应该足够快。还是我误解了?

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);       
    final Cursor c = managedQuery(Contacts.CONTENT_URI, 
                                new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, 
                                Contacts.DISPLAY_NAME + " != ?", new String[] {"NULL"}, 
                                Contacts.DISPLAY_NAME );

    String[] from = new String[] { android.provider.ContactsContract.Contacts.DISPLAY_NAME };
    int[] to = new int[] { R.id.nativecontacts_entry_name };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.rawlayout, c, from, to);

    setListAdapter(adapter);
}