列表视图中未正确显示组计数

时间:2014-01-08 08:18:49

标签: java android count

我正在显示群组名称的列表视图,其中包含群组中有多少联系人的数量。但是它没有正确显示。同时向群组添加联系人,对于一个联系人,其影响其他群组计数也增加了一倍还

它没有显示该组的正确计数,我只在2.2版本中检查这个,因为4.2此代码工作正常,但它不能在较低版本中工作

  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();

    }

0 个答案:

没有答案