0

i'm making a listview containing checkbox. i'm referring a code and try to fowllow, but can't understand one part.

refference addr : Android: CursorAdapter, ListView and CheckBox

in the below code, ther are two CheckBoxes. 1. final CheckBox cBox = (CheckBox) inView.findViewById(R.id.bcheck); 2. CheckBox cb = (CheckBox) v.findViewById(R.id.your_checkbox_id);

i understand first one, which is getting from inflater layout. but second one, i dont understand where the CheckBox id(R.id.your_checkbox_id) comes from.. could anybody help me to understand?

thanks!

 public class MyDataAdapter extends SimpleCursorAdapter { 
    private Cursor c; 
    private Context context; 
    private ArrayList<String> list = new ArrayList<String>(); 
    private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>(); 

    // itemChecked will store the position of the checked items. 

    public MyDataAdapter(Context context, int layout, Cursor c, String[] from, 
        int[] to) { 
            super(context, layout, c, from, to); 
            this.c = c; 
            this.context = context; 

            for (int i = 0; i < this.getCount(); i++) { 
               itemChecked.add(i, false); // initializes all items value with false 
            } 
    } 

    public View getView(final int pos, View inView, ViewGroup parent) { 

        if (inView == null) { 
           LayoutInflater inflater = (LayoutInflater) context 
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
           inView = inflater.inflate(R.layout.your_layout_file, null); 
        } 

        final CheckBox cBox = (CheckBox) inView.findViewById(R.id.bcheck); // your 
        // CheckBox 
        cBox.setOnClickListener(new OnClickListener() { 

           public void onClick(View v) { 

              CheckBox cb = (CheckBox) v.findViewById(R.id.your_checkbox_id);   //what is this checkbox? 
              if(cb.isChecked()) { 
                  itemChecked.set(pos, true); 
                  // do some operations here 
              }   
              else if (!cb.isChecked()) { 
                  itemChecked.set(pos, false); 
                  // do some operations here 
              }
          }
      }
   }
Community
  • 1
  • 1
beginners
  • 253
  • 1
  • 4
  • 18

1 Answers1

1

I think,it is just to know which checkbox is checked.It is used to save the correct status(checked or unchecked) of the checkbox you clicked.But i think,you can directly try as below.(I didn't try it but i should work,i think!)

Try using it like:

  public View getView(final int pos, View inView, ViewGroup parent) { 

        if (inView == null) { 
           LayoutInflater inflater = (LayoutInflater) context 
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
           inView = inflater.inflate(R.layout.your_layout_file, null); 
        } 

        final CheckBox cBox = (CheckBox) inView.findViewById(R.id.bcheck); // your 
        // CheckBox 
        cBox.setOnClickListener(new OnClickListener() { 

           public void onClick(View v) { 

              if(cBox.isChecked()) { 
                  itemChecked.set(pos, true); 
                  // do some operations here 
              }   
              else{ 
                  itemChecked.set(pos, false); 
                  // do some operations here 
              }
          }
      }
   }
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • 1
    i actually use custom cusor adapter.. so not exactly getView.. but newView and bindView.. i put above code in the bindView. but im experiencing that check box is disappearing when i scroll down and back... – beginners Oct 21 '11 at 04:58
  • But why did not you put this in getView? it should be there only. – Hiral Vadodaria Oct 21 '11 at 06:49
  • oh. i thought it would work in bindView too. i'd rather recoding with getView. thank you – beginners Oct 21 '11 at 06:51