2015-11-20 64 views
5

我有一個數組列表。如何比較陣列列表與移動聯繫人

1)我的第一個ArrayList<String> some_num = new ArrayList<String>();它包含這樣的值。

[1111111111, 2222222222] 

現在我正試圖將此與我的移動聯繫人進行比較。

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
         ContactsContract.CommonDataKinds.Phone.NUMBER}; 

       Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null); 

       int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
       int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
       int j_count=0; 
       String number; 
       people.moveToFirst(); 
       do { 

        String name = people.getString(indexName); 
        number = people.getString(indexNumber); 
        j_count++; 


        // Do work... 
       } while (people.moveToNext()); 




       for(int j=0;j<=j_count;j++){ 

        if (some_num.contains(number)){ 
         // do nothing 
        } 
        else{ 
        Log.e("num",number); 
        } 
       } 

我想從手機本書中得到那些不存在於我的ArrayList中的數字。我知道在條件我必須得到數組的價值,但是當我嘗試這樣做,我沒有得到我的聯繫的其餘部分。

在此先感謝

+0

什麼是你面對這個代碼的問題,任何異常或任何錯誤? – Anjali

+0

不,我只有一個單一的聯繫人 –

回答

6

不要使用任何其他循環來比較數字。請參考以下代碼:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
         ContactsContract.CommonDataKinds.Phone.NUMBER}; 

       Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null); 

       int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
       int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
       int j_count=0; 
       String number; 
       people.moveToFirst(); 
       do { 

        String name = people.getString(indexName); 
        number = people.getString(indexNumber); 

         if (some_num.contains(number)){ 
         }else{ 
         } 

        j_count++; 


        // Do work... 
       } while (people.moveToNext()); 

您的代碼出現了什麼問題呢?

你正在比較你的數組和number變量,它包含最後一個數字引用。因爲你只獲得單一結果。

如果你仍然想使用你的代碼,然後創建用於存儲所有號碼這樣的另一個的ArrayList:

ArrayList<String> numberList = new ArrayList<String>(); 

,並在下面一行此列表使用Ĵ++之前添加的號碼;

numberList.add(number); 

現在更新您的最後一個迭代器塊這樣的工作:

 for(int j=0;j<numberList.siz();j++){ 

      if (some_num.contains(numberList.get(i)){ 
       // do nothing 
      } 
      else{ 
      Log.e("num",numberList.get(i)); 
      } 
     } 

獲取用戶的完整的細節,你可以創建模型類,其中包含了用戶的細節是這樣的:

public class UserDetails{ 
    private String userName; 
    private String userPhone; 
    private String userImage; 

    public String getUserName() { 
     return userName; 
    } 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 

    public String getUserPhone() { 
     return userPhone; 
    } 

    public void setUserPhone(String userPhone) { 
     this.userPhone = userPhone; 
    } 

    public String getUserImage() { 
     return userImage; 
    } 

    public void setUserImage(String userImage) { 
     this.userImage = userImage; 
    } 
} 

現在,您必須在您的活動中使用此模型來獲取和設置用戶的詳細信息:

ArrayList<UserDetails> mUserDetailList = new ArrayList<UserDetails>(); 

,並獲得聯繫人名稱使用此代碼:

String name = people.getString(indexName); 

現在的店鋪名稱和PhoneNumber這樣的:

UserDetails mUserModel = new UserDetails(); 

mUserModel.setUserPhone(number); 
mUserModel.setUserName(name); 

mUserDetailList.add(mUserModel); 

現在檢查該號碼是否存在:

for(int j=0;j<mUserDetailList.siz();j++){ 

      if (some_num.contains(mUserDetailList.get(i).getUserPhone()){ 
       // do nothing 
      } 
      else{ 
      Log.e("num",numberList.get(i).getUserPhone()); 
      Log.e("name",numberList.get(i).getUserName()); 
      } 
     } 

希望這會解決您的問題。

+0

感謝@Anjali爲您提供寶貴的答案。但是當我試圖使用解決方案,所以我得到異常在「如果條件」 –

+0

異常是java.lang.IndexOutOfBoundsException:索引3無效,大小爲3它是來這裏嘗試if(mysql_ar.contains(numberList。 get(j))){ //什麼都不做 } else { Log.e(「num」,numberList.get(j)); } } catch(Exception ex){} –

+0

更新您的代碼,以便我們檢查代碼 – Anjali

1

一個理想的解決方案是使用搭扣地圖

將整個聯繫人存儲在哈希映射中作爲密鑰 ,並通過簡單地將值放入哈希映射來檢查您的列表是否具有這些數字。

這將作爲哈希表不會有duplicatesand當u試圖插入重複鍵,你就會知道,

編輯 我覺得你的代碼可能有這方面的工作解決

   String number; 
       people.moveToFirst(); 
       do { 

        String name = people.getString(indexName); 
        number = people.getString(indexNumber); 
       // for(int j=0;j<=j_count;j++){ 

        if (some_num.contains(number)){ 
         // do nothing 
        } 
        else{ 
        Log.e("num",number); 
        //} 
        j_count++; 


        // Do work... 
       } while (people.moveToNext()); 





       } 
+0

感謝@Abx其實我正在考慮使用哈希映射或集合,但我更新使用它之前,你可以請給我更多的想法如何使用 –

+1

@Abx即使你的代碼是錯誤的。如果您正在比較當前數字,那麼迭代j需要什麼? – Anjali

+0

@Anjali明白了你的觀點......已編輯它 – Abx

1

嘗試如下,不要忘了清除列表,當過水再利用

ArrayList<String> list = new ArrayList<String>(); 

people.moveToFirst(); 
       do { 

        String name = people.getString(indexName); 
        number = people.getString(indexNumber); 
        list.add(number); 
        j_count++; 


        // Do work... 
       } while (people.moveToNext()); 

for(String str:list) 
{ 

    if(yournumber.equalsIgnoreCase(str)) 
{ 
    //do your stuff 
} 

} 
+0

謝謝@venkatesh爲您的答案,但我有一個疑問是,在「如果條件」你給一個變量是yournumber但在我的問題yournumber是一個數組列表是我比較它是這樣嗎? –

+0

Nopes,它取決於你必須進行比較。列表中有你正在從光標讀取的所有數字,for循環中可以比較列表中的每個和evry項目 –