2015-06-24 17 views
0

我試圖使用ContactsProvider與我的AutoCompleteTextview使用一種方法來獲取數據(名稱和電話號碼)並將它們存儲在列表中。正如預期的那樣,此方法將總是需要時間來完成作爲我打電話,我Fragment類的onCreateView方法的方法。Android Contacts與AutocompleteTextview的合同太慢

這是方法:

... 
ArrayList<String> phoneValues; 
ArrayList<String> nameValues; 
... 

private void readContactData() { 

    try { 

     /*********** Reading Contacts Name And Number **********/ 

     String phoneNumber = ""; 
     ContentResolver contentResolver = getActivity() 
       .getContentResolver(); 

     //Query to get contact name 

     Cursor cursor = contentResolver 
       .query(ContactsContract.Contacts.CONTENT_URI, 
         null, 
         null, 
         null, 
         null); 

     // If data data found in contacts 
     if (cursor.getCount() > 0) { 

      int k=0; 
      String name = ""; 

      while (cursor.moveToNext()) 
      { 

       String id = cursor 
         .getString(cursor 
           .getColumnIndex(ContactsContract.Contacts._ID)); 
       name = cursor 
         .getString(cursor 
           .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

       //Check contact have phone number 
       if (Integer 
         .parseInt(cursor 
           .getString(cursor 
             .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
       { 

        //Create query to get phone number by contact id 
        Cursor pCur = contentResolver 
          .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
            null, 
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
              + " = ?", 
            new String[] { id }, 
            null); 
        int j=0; 

        while (pCur 
          .moveToNext()) 
        { 
         // Sometimes get multiple data 
         if(j==0) 
         { 
          // Get Phone number 
          phoneNumber =""+pCur.getString(pCur 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

          // Add contacts names to adapter 
          autocompleteAdapter.add(name); 

          // Add ArrayList names to adapter 
          phoneValues.add(phoneNumber.toString()); 
          nameValues.add(name.toString()); 

          j++; 
          k++; 
         } 
        } // End while loop 
        pCur.close(); 
       } // End if 

      } // End while loop 

     } // End Cursor value check 
     cursor.close(); 


    } catch (Exception e) { 
     Log.i("AutocompleteContacts","Exception : "+ e); 
    } 


} 

相信有更好的方式來做到這一點,但這種方法的作品,當我鍵入到AutocompleteTextview的建議都。我只是擔心所花費的時間。如何在不填充ArrayList的情況下完成此操作? 我已經看過了這個問題:Getting name and email from contact list is very slow和應用的答案建議我的代碼,但現在當我我type.How可以提高我目前的代碼的性能沒有什麼建議?

+0

「我怎樣才能做到這一點不填充的ArrayList?」使用'SimpleCursorAdapter'也需要多長時間?多少聯繫人?數據集獨立於視圖層次結構。將retain實例設置爲true,並通過'CursorLoader'異步獲取數據到'onCreate'中。 – pskink

+0

@pskink當我點擊一個按鈕導航到片段,大約需要8秒導航到該片段 –

+0

了多少個聯繫人,你呢? – pskink

回答

0

這是多麼我已經實現AutoCompleteTextView並能正常工作對我來說..

final AutoCompleteTextView act=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); 
     ArrayList<String> alContacts = new ArrayList<String>(); 
     ArrayList<String> alNames= new ArrayList<String>(); 

ContentResolver cr = this.getContentResolver(); //Activity/Application android.content.Context 
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
if(cursor.moveToFirst()) 
{ 

    do 
    { 
     String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 

     if(Integer.parseInt(cursor.getString(cursor.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()) 
      { 
       String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       String contactName=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       alNames.add(contactName); 
       alContacts.add(contactNumber); 
       break; 
      } 
      pCur.close(); 
     } 

    } while (cursor.moveToNext()) ; 
} 


String[] array=new String[alNames.size()]; 
array=(String[]) alNames.toArray(array); 
ArrayAdapter<String> myArr= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,array); 
act.setAdapter(myArr); 
act.setThreshold(1); 
+0

你在哪裏放置這段代碼?在你的onCreate()? –

+0

是的,在onCreate().. –

+0

不幸的是,這並沒有爲我工作 –

0

我被放置AsynTask裏面的方法去掉反應遲緩。

public class AutocompleteAsyncTask extends AsyncTask<Void, Void, Void> { 


    public Void doInBackground(Void...params) { 

     try { 

      /*********** Reading Contacts Name And Number **********/ 

      String phoneNumber = ""; 
      ContentResolver contentResolver = getActivity() 
        .getContentResolver(); 

      //Query to get contact name 

      Cursor cursor = contentResolver 
        .query(ContactsContract.Contacts.CONTENT_URI, 
          null, 
          null, 
          null, 
          null); 

      // If data data found in contacts 
      if (cursor.getCount() > 0) { 

       int k=0; 
       String name = ""; 

       while (cursor.moveToNext()) 
       { 
      //...Rest of the same code as above 

,然後在我onCreateView()調用此:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 

    Bundle savedInstanceState) { 
... 
    new AutocompleteAsyncTask().execute(); 
    return rootView; 
} 

現在充氣我的觀點和獲取數據的任務被分成兩個不同的線程。

Eugen Pechanec提到的CursorLoader選項是最好的選擇,所以我會更新這個答案,當我可以。

+0

正如我所說的,使用SimpleCursorAdapter這裏http://pastebin.com/fdg3QmAq你有試過it.Am得到一個錯誤,完整的工作代碼 – pskink

+0

魔術17行:'指定的孩子已經有一個父。你必須首先調用孩子父母的removeView()。但是當我註釋掉代碼的第二行時,它會起作用,但會返回一個奇怪的文本:'android.content.contentresolver $ CursorWrapperInner @ 41b78fd8' –

+0

不要評論第二行線,只需使用一個的setContentView在onCreate方法 – pskink