0

I am showing the listview of the group name with count of how many contacts are there in the group.But its not showing correctly.while adding contact to the group ,for one contacts its affecting other group count also and its increasing double also.

Its not showing the correct count of the group,i am checking this only in 2.2 version,because 4.2 this code is working perfectly,but its not working in lower versions

  public void onResume()
             {
              super.onResume();
               getgroup=getAvailableContactGroups();
                  getcount();

                  gAdapter = new GroupAdapter(this, getgroup);

            lv.setAdapter(gAdapter);
    }

class GroupAdapter extends BaseAdapter 
        {  
            private SparseBooleanArray mCheckStates;
            ArrayList<String> group; 


            LayoutInflater mInflater;
            TextView tv,tv1;
            CheckBox cb;
            GroupAdapter(FirstScreenGroupActivity groupActivity, ArrayList<String> group)
            {
                mCheckStates = new SparseBooleanArray(group.size());
                mInflater = (LayoutInflater)FirstScreenGroupActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                this.group = group;


            }
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return group.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub

                return 0;
            }


    public View getView(final int position, View convertView, ViewGroup parent) {
                    // TODO Auto-generated method stub
                    View vi=convertView;
                    if(convertView==null)

                    vi = mInflater.inflate(R.layout.show_groups, null); 
                    tv= (TextView) vi.findViewById(R.id.textView2);
                    tv1= (TextView) vi.findViewById(R.id.groupcount);


                    try
                    {
                    tv.setText(group.get(position));

                    tv1.setText("(" + String.valueOf(groupcount.get(position)) + ")");
                    }
                    catch(IndexOutOfBoundsException e)
                    {
                        Toast.makeText(FirstScreenGroupActivity.this, "catch", 1000).show();
                    }


                    return vi;
                }


getCount

    void getcount()
    {


         boolean status=false;
            String selection = ContactsContract.Groups.DELETED + "=? and " + ContactsContract.Groups.GROUP_VISIBLE + "=?";
            String[] selectionArgs = { "0", "1" };
            Cursor cursor = this.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, selection, selectionArgs, null);

            if(cursor.moveToFirst())
            {
                do
                {
                String title = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups.TITLE));


                  int len = cursor.getCount();

            ArrayList<String> numbers = new ArrayList<String>();
            for (int i = 0; i < len; i++)
            {
               // String title = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups.TITLE));
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups._ID));
              // Toast.makeText(this,"group:"+title+":id"+ id,1000).show();

                {
                    String[] cProjection = { Contacts.DISPLAY_NAME, GroupMembership.CONTACT_ID };

                    Cursor groupCursor = this.getContentResolver().query(
                            Data.CONTENT_URI,
                            cProjection,
                            CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND "
                                    + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='"
                                    + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
                            new String[] { String.valueOf(id) }, null);
                    if (groupCursor != null && groupCursor.moveToFirst())
                    {
                        int count = 0;
                        do
                        {
                        //count = 0;
                            int nameCoumnIndex = groupCursor.getColumnIndex(Phone.DISPLAY_NAME);

                            String name = groupCursor.getString(nameCoumnIndex);

                            long contactId = groupCursor.getLong(groupCursor.getColumnIndex(GroupMembership.CONTACT_ID));

                            Cursor numberCursor = this.getContentResolver().query(Phone.CONTENT_URI,
                                    new String[] { Phone.NUMBER }, Phone.CONTACT_ID + "=" + contactId, null, null);

                            if (numberCursor.moveToFirst())
                            {
                                 int numberColumnIndex = numberCursor.getColumnIndex(Phone.NUMBER);
                                    do
                                    {
                                        count++;
                                        String phoneNumber = numberCursor.getString(numberColumnIndex);
                                        //Toast.makeText(this,phoneNumber,1000).show();
                                        numbers.add(phoneNumber);
                                    } while (numberCursor.moveToNext());
                                    numberCursor.close();


                                }
                            //count = 0;
                            } while (groupCursor.moveToNext());
                          groupcount.add(count);

                          count=0;

                           groupCursor.close();

                    }
                    else
                    {
                        groupcount.add(0);
                        //Toast.makeText(this,"no contacts are there",1000).show();
                    }
                    break;
                }


            }


            }while(cursor.moveToNext());

                }


            cursor.close();

    }
ekad
  • 14,436
  • 26
  • 44
  • 46
Karthick M
  • 769
  • 2
  • 11
  • 29

0 Answers0