sql android忽略重复检索内容

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql android忽略重复检索内容相关的知识,希望对你有一定的参考价值。

帮我解决这个问题,以删除我从数据库中获得的重复联系人

 cursor= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                 ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.DATA1 + "!=''",null,"upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
答案

use this code to contact list from mobile database and set on list view

No duplicate data retreive.

public void customgetContactList()
{
    String SELECTION = "((" +
            ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +
            ContactsContract.Data.DISPLAY_NAME + " != '"+checkDuplicacy+"' ))";


    Cursor cursor= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
            SELECTION,null,"upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

    int ColumeIndex_ID = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
        int ColumeIndex_DISPLAY_NAME = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int ColumeIndex_HAS_PHONE_NUMBER = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

    ArrayList<HashMap<String,String>> arrayList=new ArrayList<>();
    while (cursor.moveToNext()){
        String id = cursor.getString(ColumeIndex_ID);
        String name = cursor.getString(ColumeIndex_DISPLAY_NAME);
        String has_phone = cursor.getString(ColumeIndex_HAS_PHONE_NUMBER);

        //phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
        if (!name.equals(checkDuplicacy) && !has_phone.equals(checkDuplicacynumber))
        {

            HashMap<String,String> hashMap=new HashMap<>();//create a hashmap to store the data in key value pair
            hashMap.put("name",name);
            hashMap.put("phone",has_phone);
            arrayList.add(hashMap);
            String[] from = {"name","phone"};
            int[] to = {R.id.textViewName,R.id.textviewnumber};
            SimpleAdapter simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.customtextviewforlist,from,to);
          //  SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.customtextviewforlist,cursor,from,to);

            //startManagingCursor(cursor);
            contactListView.setAdapter(simpleAdapter);
            contactListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


            checkDuplicacy=name;
            checkDuplicacynumber=has_phone;
        }
    }
}

以上是关于sql android忽略重复检索内容的主要内容,如果未能解决你的问题,请参考以下文章

SQL检索,但检索结果中有某字段内容重复的数据只保留1条显示

用于从 cloudkit 检索单列的代码模式/片段

全局变量在android studio中返回null [重复]

片段android中未调用onActivityResult [重复]

将数据从活动发送到片段android工作室[重复]

片段中的Android Studio RecyclerView [重复]