[주소록]그룹 카운팅 하기

안드로이드/android 2012. 12. 10. 08:58

같은 이름으로 저장된 그룹을 하나의 그룹명으로 카운트하고

Data에 실질적으로 등록된 연락처 카운터를 한다.



// group

private HashMap<String, String> group_kr;

private HashMap<String, String> group_title;

private HashMap<String, Integer> group_count;

@Override

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        setContentView(R.layout.phonebook);    

   

    group_title=new HashMap<String, String>();

    group_count=new HashMap<String, Integer>();

    getGroupContacts();


    }

private void getGroupContacts()

{

Uri uri_group = ContactsContract.Groups.CONTENT_SUMMARY_URI;

String[] group_projection = new String[]{

ContactsContract.Groups._ID,

ContactsContract.Groups.TITLE

};

String group_selection = ContactsContract.Groups.DELETED + " = 0 AND " + ContactsContract.Groups.GROUP_VISIBLE + " = 1";

String orderby = ContactsContract.Groups.TITLE+ " COLLATE LOCALIZED ASC";

   Cursor gc = managedQuery(uri_group, group_projection, group_selection, null,orderby);

   

   while(gc.moveToNext())

   {

    String gtitle=gc.getString(1);

    if(gtitle!=null && !gtitle.equals(""))

    {

    int people_count=0;


    String selection=ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "="+gc.getString(0);

//     String[] qry={ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID};

//     Cursor people_rlt = managedQuery(ContactsContract.Data.CONTENT_URI,qry, selection, null,null);//    

//     final int people_count = people_rlt.getCount();

//     Log.i("getGroupContacts",gc.getString(0)+" "+gc.getString(1)+" ("+people_count+")");

   

    Uri lookupUri = Uri.withAppendedPath(ContactsContract.Data.CONTENT_URI, "");

   Cursor is_c = getContentResolver().query(lookupUri, new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID},selection,null,null);

   people_count=is_c.getCount();

   try {

    people_count=is_c.getCount();

   } catch (Exception e) {}

   finally{is_c.close();}

   

    if(group_title.get(gtitle)!=null)

    {

    int g_tcount = group_count.get(gtitle)+people_count;

    group_count.put(gtitle,g_tcount);

    }else{

    group_title.put(gtitle,gtitle);

    group_count.put(gtitle,people_count);

    }

    }

   }

   Log.i("Group list",group_count.toString());

}