0

How to get the selected item from ListView (selected view from a checkbox)? I got some results from the Handler in the CursorAdapter.

public class MyCursorAdapter extends CursorAdapter {

@SuppressWarnings("deprecation")
public MyCursorAdapter(Context context, Cursor c) {
    super(context, c);
    // TODO Auto-generated constructor stub
}

@Override
public void bindView(View view, final Context context, Cursor cursor) {
    TextView item=(TextView)view.findViewById(R.id.item_tv);
    CheckBox cb=(CheckBox)view.findViewById(R.id.flag_cb);

    item.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

    Log.d("Bind View", "Flag="+cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));

    if(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))).equalsIgnoreCase("true")){
        cb.setChecked(true);
    }else{
        cb.setChecked(false);
    }

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            DatabaseHelperSQL help=new DatabaseHelperSQL(context);
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                Toast.makeText(context, "Check", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(context, "unCheck", Toast.LENGTH_LONG).show();
            }
        }   

    });

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater=LayoutInflater.from(parent.getContext());
    View retView=inflater.inflate(R.layout.every_row,parent,false);
    return retView;
}

}
Patt Mehta
  • 4,110
  • 1
  • 23
  • 47
Sathish
  • 2,056
  • 3
  • 26
  • 40
  • http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button/18164177#18164177. answered a similar one. check this – Raghunandan Aug 15 '13 at 09:41

1 Answers1

0

It depends on what you specifically need:

If you need the ids of selected items, you can use boolean[] selected, which will store the state of each item.

If you need to get fully selected items, you can use List<View> selected

Put in onCheckedChanged
And return when it needed

andreich
  • 1,120
  • 1
  • 11
  • 28