2012-02-04 87 views
0

我有代碼來讀取聯繫人,獲取名稱和編號,並將它們放在listView中。一切正常,但應用程序在同一時間讀取所有聯繫人。我想我必須閱讀他們的滾動移動,但我現在不怎麼樣,請幫忙? 先謝謝你,沃爾夫。優化閱讀listView中的聯繫人?

這是我的代碼:

的ArrayList>地圖=新的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); 

回答

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

事情是,我嘗試在我的手機上應用程序,我有700個聯繫人的東西后,我必須等待15秒,然後我得到listView中的所有聯繫人。 – Wolf87 2012-02-04 20:14:01

+0

如果可能,我希望這樣做更快? – Wolf87 2012-02-04 20:14:45

+0

但您的代碼正在做一些不必要的事情:複製地圖中的聯繫人。您可以直接使用光標 – kingston 2012-02-05 10:39:40