2016-10-14 65 views
0

我已經開發了應用程序,通過內容解析器訪問聯繫人。它顯示了一個列表視圖中的所有聯繫人甚至重複的聯繫人也顯示在同一個列表視圖但我想在其他列表中顯示重複的聯繫人查看,以便我可以根據自己的願望輕鬆刪除它們。請幫助我。我謹向你致以誠摯的謝意。這裏有示例代碼。如何在列表中查找重複的聯繫人查看

package com.example.contentprovider; 

import android.content.ContentResolver; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.MatrixCursor; 
import android.provider.ContactsContract; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 


public class MainActivity extends AppCompatActivity { 

// Cursor Adapter for storing contacts data 
SimpleCursorAdapter adapter; 

// List View Widget 
ListView lvContacts; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Init ListView 
    lvContacts = (ListView) findViewById(R.id.lvContacts); 

    // Initialize Content Resolver object to work with content Provider 
    ContentResolver cr = getContentResolver(); 

    // Read Contacts 
    Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI, 
      new String[] { ContactsContract.Contacts._ID, 
        ContactsContract.Contacts.DISPLAY_NAME }, null, null, 
      null); 


    // Attached with cursor with Adapter 
    adapter = new SimpleCursorAdapter(this, R.layout.row, c, 
      new String[] { ContactsContract.Contacts.DISPLAY_NAME }, 
      new int[] { R.id.lblName }); 

    // Display data in listview 
    lvContacts.setAdapter(adapter); 

} 
} 

回答

1

我建議您將所有聯繫人添加到列表中,並通過hashset刪除重複。因爲哈希集不允許重複元素。

psedo code:--- 
1. Once get all object like name,phone number from contentResolver then add those string objet into arraylist 
2. After that pass taht list to hashset so duplicate will be removed. 

ArrayList<String> values=new ArrayList<String>(); 
HashSet<String> hashSet = new HashSet<String>(); 
hashSet.addAll(values); 
values.clear(); 
values.addAll(hashSet); 
it might be helpful for you . 
+0

你可以修改我的代碼,我可以不合格 –

+0

我不明白這個代碼和它放在哪裏。請修改我的代碼。 –

+0

我修改了我的答案 –

相關問題