3

I'm new to programming and having problem with using getCheckedItemPositions() for check boxes and getting edit values from edit text in custom list view. Can anyone help me with an example to create custom list view which has above functionality. Thanks.

*this is custom listview xml code

<CheckBox android:id="@+id/checkBox" android:layout_height="wrap_content"
 android:layout_width="wrap_content" 
 android:layout_alignParentRight="true"
 android:text="CheckBox"
 android:onClick="clickHandler"
 ></CheckBox>
<TextView android:layout_height="wrap_content"
 android:layout_width="fill_parent" android:text="TextView" 
 android:id="@+id/textView1" 
 android:layout_alignParentLeft="true"
  android:layout_centerHorizontal="true" android:layout_marginBottom="14dp"

android:layout_toLeftOf="@+id/checkBox">

*This is code for set up listview

lv1 = (ListView)dialog.findViewById(R.id.l1); adapter2 = new SimpleCursorAdapter( this, R.layout.custom, cursor2, new String[] {"ItemName"}, new int[] {R.id.textView1});

         lv1.setItemsCanFocus(false);
         lv1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
         lv1.setAdapter(adapter2);

*this what I tried to do when checked

public void onClick(View v) {

                int len = lv1.getCount();
                 SparseBooleanArray checked = lv1.getCheckedItemPositions();
                 for (int i = 0 ; i < len; i++)
                  if (checked.get(i)) {
                   String item = mArrayList.get(i);

                mItems.add(mArrayList.get(i));

             System.out.println(item);




                   /* do whatever you want with the checked item */
                  } 

But this is not working. And also I want to use edit text in this manner for getting values.when I checked and click the button app terminate. Logcat when Onclick() trigurred

6
  • Show us what you have tried so we can tell you where you went wrong... we're not going to do your coding for you.
    – Barak
    May 17, 2012 at 4:10
  • @Barak the code i tried is above ..thanks
    – Akz Li
    May 17, 2012 at 4:35
  • @AlexLockwood the code i tried is above ..thanks
    – Akz Li
    May 17, 2012 at 4:36
  • Ok, thanks for the code, now please define "not working". What is (or is not) happening that you expect (or don't expect)?
    – Barak
    May 17, 2012 at 4:44
  • Please post your logcat too, that'll tell us where to look.
    – Barak
    May 17, 2012 at 4:53

1 Answer 1

1

The following code will solve your problem.

public class SimpleCursorAdapter extends ArrayAdapter {
    private Context mcontext;   
    private View rowview;
    LayoutInflater inflater;
    public static ArrayList<Boolean > itemchecked=new ArrayList<Boolean>();
    public SimpleCursorAdapter(Context context,ArrayList<String> mylist)
    {
        super(context,your layout id);
        mcontext=context;

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

    public View getView(final int position, View convertView, ViewGroup parent) {
        rowview=convertView;
        if(convertView==null)
        {
            rowview = inflater.inflate(R.yourlayout, parent, false);
        }

        TextView textView_heading = (TextView) rowview.findViewById(R.id.textView1);


        CheckBox checkbox_detail=(CheckBox) rowview.findViewById(R.id.checkBox1);
        checkbox_detail.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);

                if (cb.isChecked()) {
                    itemchecked.set(position, true);
                    // do some operations here
                } else if (!cb.isChecked()) {
                    itemchecked.set(position, false);
                    // do some operations here
                }   
            }
        });
        checkbox_detail.setChecked(itemchecked.get(position));
        textView_heading.setText(userheading_list.get(position));

        return rowview;
    }   
}
//now the custom list part finish

Now, to get all the information from list and also watch which checkbox is checked:

for(int i=0;i<yourlistadapterobject.getCount();i++)
{  
    View content_view=msg_adapter.getView(i,null , user_detail_list);
    System.out.println("the list count"+user_detail_list.getCount());
    if(MyContactAdapter.itemchecked.get(i))
    {
        System.out.println("is checked true"); 
        TextView tv_heading=  (TextView) content_view.findViewById(R.id.textView1);

        String text=tv_heading.getText();
    }
}

By doing this, you can get all the information against checkbox which is checked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.