i've been doing research on can't figure out. contacts in phone in sample app downloaded duplicated, so:
i'm quite sure has contactscontract.contacts. i've read on don't know how implement in code. (or indeed if there's way of doing it). want each contact listed once, not multiple time.
according http://developer.android.com/reference/android/provider/contactscontract.contacts.html :
contactscontract.contacts constants contacts table, contains record per aggregate of raw contacts representing same person.
i have 3 java files in project, mainactivity, selectuser , selectuseradapter, believe mainactivity 1 pertaining problem. line :
phones = getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, null, null, null, contactscontract.commondatakinds.phone.display_name + " asc");
if need more code let me know.
here's mainactivity.java :
package com.example.chris.contactlistcustomlistview; import android.app.activity; import android.content.contentresolver; import android.database.cursor; import android.graphics.bitmap; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.provider.contactscontract; import android.provider.mediastore; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.listview; import android.widget.searchview; import android.widget.toast; import java.io.ioexception; import java.util.arraylist; import java.util.list; public class mainactivity extends activity { // arraylist arraylist<selectuser> selectusers; list<selectuser> temp; // contact list listview listview; // cursor load contacts list cursor phones, email; // pop contentresolver resolver; searchview search; selectuseradapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); selectusers = new arraylist<selectuser>(); resolver = this.getcontentresolver(); listview = (listview) findviewbyid(r.id.contacts_list); phones = getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, null, null, null, contactscontract.commondatakinds.phone.display_name + " asc"); // retrieves contact information loadcontact loadcontact = new loadcontact(); loadcontact.execute(); // let's set our search box, search = (searchview) findviewbyid(r.id.searchview); //*** setonquerytextlistener *** search.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string query) { // todo auto-generated method stub return false; } @override public boolean onquerytextchange(string newtext) { // when text in searchview changes, call filter function adapter.filter(newtext); return false; } }); } // load data on background class loadcontact extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(void... voids) { // contact list phone if (phones != null) { log.e("count", "" + phones.getcount()); if (phones.getcount() == 0) { toast.maketext(mainactivity.this, "no contacts in contact list.", toast.length_long).show(); } while (phones.movetonext()) { bitmap bit_thumb = null; string id = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.contact_id)); string name = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.display_name)); string phonenumber = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.number)); string emailaddr = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.email.data2)); string image_thumb = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.photo_thumbnail_uri)); try { if (image_thumb != null) { bit_thumb = mediastore.images.media.getbitmap(resolver, uri.parse(image_thumb)); } else { log.e("no image thumb", "--------------"); } } catch (ioexception e) { e.printstacktrace(); } //what's happening here? every user in phonebook, show image, name, number, id , maybe checkbox? selectuser selectuser = new selectuser(); selectuser.setthumb(bit_thumb); selectuser.setname(name); selectuser.setphone(phonenumber); selectuser.setemail(id); selectuser.setcheckedbox(false); selectusers.add(selectuser); } } else { log.e("cursor close 1", "----------------"); } //phones.close(); return null; } @override // when doinbackground finished, when have our phone number, name etc... display results in our listview. protected void onpostexecute(void avoid) { super.onpostexecute(avoid); adapter = new selectuseradapter(selectusers, mainactivity.this); listview.setadapter(adapter); // select item on listclick listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { log.e("search", "here---------------- listener"); selectuser data = selectusers.get(i); } }); listview.setfastscrollenabled(true); } } @override protected void onstop() { super.onstop(); phones.close(); } }
public class mainactivity extends activity { cursor cursor; listview mainlistview; arraylist hashmapsarraylist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (cursor != null) { cursor.movetofirst();} try { cursor = getapplicationcontext().getcontentresolver() .query(contactscontract.commondatakinds.phone.content_uri, null, null, null, null); int idx = cursor.getcolumnindex(contactscontract.commondatakinds.phone.contact_id); int nameidx = cursor.getcolumnindex(contactscontract.commondatakinds.phone.display_name); int phonenumberidx = cursor.getcolumnindex(contactscontract.commondatakinds.phone.number); int photoididx = cursor.getcolumnindex(contactscontract.commondatakinds.phone.photo_thumbnail_uri); cursor.movetofirst(); set<string> ids = new hashset<>(); { system.out.println("=====>in while"); string contactid=cursor.getstring(idx); if (!ids.contains(contactid)) { ids.add(contactid); hashmap<string, string> hashmap = new hashmap<string, string>(); string name = cursor.getstring(nameidx); string phonenumber = cursor.getstring(phonenumberidx); string image = cursor.getstring(photoididx); system.out.println("id--->"+contactid+"name--->"+name); system.out.println("id--->"+contactid+"name--->"+name); system.out.println("id--->"+contactid+"number--->"+phonenumber); if (!phonenumber.contains("*")) { hashmap.put("contactid", "" + contactid); hashmap.put("name", "" + name); hashmap.put("phonenumber", "" + phonenumber); hashmap.put("image", "" + image); // hashmap.put("email", ""+email); if (hashmapsarraylist != null) { hashmapsarraylist.add(hashmap);} // hashmapsarraylist.add(hashmap); } } } while (cursor.movetonext()); } catch (exception e) { e.printstacktrace(); } { if (cursor != null) { cursor.close(); } } } }
Comments
Post a Comment