0

I have a listview with some check boxes and i need to check them automatically according to some data that are stored in array. My custom adapter that extends base adapter:

 public class MyAdapter extends BaseAdapter 
    {
        private Context context;

        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

        public int getCount() {
            return MyArrList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public int getlistItemsCount()
        {
            return listView.getChildCount();
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null);

            }   
            // ColID
            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") +".");

            // ColCode
            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));


            // ColChk               
            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
            Chk.setTag(MyArrList.get(position).get("RbMjere"));


            return convertView;

        }

    }

And this is how i check the items

int k=0;
    int j=0;
    for (j=0; j<numberOfItems; j++)
    {

        LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); // Find by under LinearLayout
        CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);

        for (k=0; k<rbmjere.size(); k++)
        {
            if (checkbox.getTag().toString() == rbmjere.get(k).toString())
            {
                checkbox.setChecked(true);
            }
        }   
    }

The problem is at line

LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j);

So if I call this code for checking items the problem is that listview show for example 3 items but the code recognize only 2 items and third item is missing. How to detect when all items are visible or how to detect when rendering of listview is finished?

Josef
  • 2,430
  • 5
  • 29
  • 56
  • 3
    I suggest you to use `int count = listView.getChildCount();` and then `for (j=0; j – user370305 Jul 29 '13 at 09:05
  • 1
    Look at your `for` loop - it only loops twice - for 0 and 1 (it's <2 , so 2 is not included) :) – g00dy Jul 29 '13 at 09:06
  • But where to put that? I want to check items after listview load all data. I'm not sure if my logic is right... – Josef Jul 29 '13 at 09:08
  • The question is where to put that loop? Inside getView() or somewhere else? – Josef Jul 29 '13 at 09:09
  • 1
    You don't need to cast to LinearLayout to use findViewById. You don't need to cast into Checkbox to use setTag. numberOfItems should be obtainec as listView.getChildCount which returns items visible in ListView (not to comfuse with "all items are there to be created with length of adapter.getCount") What is this - checkbox.getTag().toString() == rbmjere.get(k).toString() ? Strings can't be compared like this. If you want to compare object instances, you don't have to convert them to Strings. If you want to compare String values, you should use Object.equals() method of Strings. – Yaroslav Mytkalyk Jul 29 '13 at 09:27
  • but getChildCount always counts one less than actual number of items but only in situation when I call this function for automatic checking boxes from getView(). So it never checks all boxes. – Josef Jul 29 '13 at 10:04

3 Answers3

0

Try to use Array Adapter instead of base adapter or something like this:

public class ArrayListAdapter<T> extends ArrayAdapter<T> {



     private List<T> arrayList;

        public ArrayListAdapter(Context context, int layout, List<T> arrayList) {
            super(context, layout);
            this.arrayList = arrayList;
        }

        @Override
        public T getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public int getCount() {
            return arrayList.size();
        }
    }

create instance of adapter and set listView.setAdapter(arrayAdapter); I see you don't provide List to adapter its propably why you get nullPointerException

Mathew1990
  • 327
  • 2
  • 17
0

1.Maybe the adapter had not set the listview yet

Handler handler = new Handler();        
    handler.postDelayed(new Runnable() {            
        public void run() {
              //your code
              CheckBox checkbox = (CheckBox)listView.getChildAt(j).findViewById(R.id.ColChk);
              //your code
        }
    }, 500);

2.Maybe you scroll the listview so the index is wrong, reference this Android ListView y position

Community
  • 1
  • 1
Gina
  • 904
  • 8
  • 15
  • For example if I call the function for checking from OnClickListener() of button widget that boxes are correctly checked, but if I call it from getView() then always one of checkboxes is not checked. Then I put counter inside of getView to count how much items is there. If I load 3 items the getView counts 2 but listview shows correctly three items. – Josef Jul 29 '13 at 10:01
  • http://stackoverflow.com/questions/5687077/android-unable-to-check-all-the-checkboxes-in-a-custom-listview-because-of-rec – Gina Jul 30 '13 at 01:42
0

Problem is solved. Inside getView() i wrote these few lines and it works just I wanted:

for (k=0; k<rbmjere.size(); k++)
            {
                if (Chk.getTag().toString().equals(rbmjere.get(k).toString()))
                {
                    Chk.setChecked(true);

                }
            }   

Thanks to all on help and ideas.

Josef
  • 2,430
  • 5
  • 29
  • 56