如何避免重复的联系人姓名(数据),而加载的联系方式,以ListView的? [英] How to avoid duplicate contact name (data ) while loading contact info to listview?

查看:173
本文介绍了如何避免重复的联系人姓名(数据),而加载的联系方式,以ListView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我填充联系人列表的详细信息,以成功列表视图。 我的code:

I am populating contact list details to list view successfully. My code:

  String order = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
  Cursor   curLog =  getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,order); 

如何避免重复的数据在列表视图中的联系方式被重复,如果它加入了接触,即加入了两个手机和谷歌?。屏幕是像

How can I avoid the duplicate data In List view as the contact details is repeating if its joined contact i.e. joined with both phone and Google?. The screen is like

我要选择编程只有1名不是两者兼而有之?任何想法如何,我可以选择?

I want to select programmatically only 1 name not the both? Any Idea how I can select?

推荐答案

我已经使用了一个粗略的办法来避免这个问题,这帮助了我这么多,工作得很好。

I have used a rough way to avoid this problem which helped me so much and working nicely.

使用本地数据库(SQLite的),以避免数据duplicacy是make电话号码是唯一的。

use Local database(SQLite) to avoid data duplicacy by make phone number to unique.

我已经做了一个的SQLite数据库来处理这个问题:

I have made one SQLite DB to handle this problem:

ContactMerger.java:

public class ContactMerger {

private static final String CONTACT_TABLE = "_contact_table";
private static final String CONTACT_ID = "_contactId";
private static final String CONTACT_NAME = "_contactName";
private static final String CONTACT_MOBILE_NUMBER = "_contactNumber";
private static final String CONTACT_DATE  = "_contactDate";


private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "DB_Contact";

private final Context context;
private SQLiteDatabase ourDatabase;
private DbHelper ourHelper;

private class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String contactQuery = "CREATE TABLE " + CONTACT_TABLE + " ("
                + CONTACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + CONTACT_NAME + " TEXT NOT NULL, " + CONTACT_DATE
                + " TEXT NOT NULL, " + CONTACT_MOBILE_NUMBER
                + " TEXT NOT NULL UNIQUE);";

        db.execSQL(contactQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + CONTACT_TABLE);
        onCreate(db);
    }

}

public ContactMerger(Context context) {
    this.context = context;
}

public ContactMerger open() throws SQLException {
    ourHelper = new DbHelper(context);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

// Insert Data to Contact Table
public long insertContacts(String name, String number, String date) throws SQLException {
    ContentValues cv = new ContentValues();
    cv.put(CONTACT_NAME, name);
    cv.put(CONTACT_DATE, date);
    cv.put(CONTACT_MOBILE_NUMBER, number);
    Log.d("Insert Data", cv.toString());
    return ourDatabase.insert(CONTACT_TABLE, null, cv);
}

//Get Contact details from Contact Table
public ArrayList<ContactHolder> getContactDetails() throws Exception{
    ArrayList<ContactHolder> contactDetails = new ArrayList<ContactHolder>();
    String[] culumns = new String[] { CONTACT_ID, CONTACT_NAME, CONTACT_DATE, CONTACT_MOBILE_NUMBER };
    Cursor c = ourDatabase.query(CONTACT_TABLE, culumns, null, null, null,null, null);

    int iContactName = c.getColumnIndex(CONTACT_NAME);  
    int iContactDate = c.getColumnIndex(CONTACT_DATE);  
    int iContactMobileNumber = c.getColumnIndex(CONTACT_MOBILE_NUMBER);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

        ContactHolder data = new ContactHolder();
        data.setName(c.getString(iContactName));
        data.setDate(c.getString(iContactDate));
        data.setNumber(c.getString(iContactMobileNumber));

        contactDetails.add(data);
    }

    return contactDetails;

 }
}

下面 ContactHolder 只是一个getter / setter方法​​的类来处理接触实体。

Here ContactHolder is just a getter/setter class to handle contact entities.

首先,我在我的MainActivity插入的所有联系信息,一旦被后台线程的帮助。它prevents插入联系人信息多次。

First I inserted all Contact information once in my MainActivity by the help of a background thread. It prevents to insert the contact info multiple times.

是这样的:

private ArrayList<ContactHolder> contactHolder;
private void setCallLogs(Cursor managedCursor) {
    contactHolder = new ArrayList<ContactHolder>();

    int _number = managedCursor
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    int _name = managedCursor
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int _id = managedCursor
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);


    while (managedCursor.moveToNext()) {

        ContactHolder holder = new ContactHolder();
        holder.setNumber(managedCursor.getString(_number));
        holder.setName(managedCursor.getString(_name));
        holder.setDate(managedCursor.getString(_id));
        contactHolder.add(holder);
    }
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            for(int i=0; i<contactHolder.size(); i++){
                try{
                    ContactMerger merger = new ContactMerger(HomeActivity.this);
                    merger.open();
                    merger.insertContacts(contactHolder.get(i).getName(),
                            contactHolder.get(i).getNumber(),
                            contactHolder.get(i).getdate());
                    merger.close();

                } catch(Exception e){
                     e.printStackTrace();
                }
            }

        }
    });

    t.start();  

}

最后,我GTT内的AsyncTask的(doInbackground())所有的联系人信息,并把适配器/列表视图在其onPostExecute()方法,我想显示类。

At last I gtt all contact information inside an Asynctask(doInbackground()) and put in adapter/listview in its onPostExecute() method in the class I want to show.

下面:

@Override
    protected ArrayList<ContactHolder> doInBackground(String... parameters) {
        ArrayList<ContactHolder> filterContacts = new ArrayList<ContactHolder>();
        ContactMerger merger = new ContactMerger(Aaja_Contact.this);
        merger.open();
        try {
            filterContacts = merger.getContactDetails();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        merger.close();

        return filterContacts;
    }

这篇关于如何避免重复的联系人姓名(数据),而加载的联系方式,以ListView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆