2

I have a ListView which is visible as like:-

(Checkbox) TEXT [EditText]

(Checkbox) TEXT [EditText]

(Checkbox) TEXT [EditText]

...

What i really want is to get the values of all edittexts which are checked in Checkbox.

I have used a class MyItem to get and set values, class MyAdpater to set Adapter and item layout and class MyView for the activity. The layout is not allowing onItemClick/onItemSelected method to work as layout is having checkbox and edittext so it didn't get clicked as a list item.

I'll add more info if required.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Cool Jatt
  • 403
  • 2
  • 6
  • 16

3 Answers3

2

As you build your ListView with your adapter, create and array list with a new CheckBox view in it for each element.

ArrayList<CheckBox> a = new ArrayList<CheckBox>(16);
ArrayList<EditText> e = new ArrayList<EditText>(16);

public class Adapterwhatever extends asdljbg {

public View getChildVieworsomething(AdapterView<> viwe, int position..ydadayda) {



 //make sure you reset these
 a.add(new (CheckBox) findViewById(R.id.whateverman));
 b.add(new (EditText) findViewById(R.id.anEditText));

}

Use the arrayLists to check for values after calling

adapter.notifyDataSetChanged();
Jack Satriano
  • 1,999
  • 1
  • 13
  • 17
2

I've done something similar in the following fashion (used as a dialog). I don't use a checkbox, I just process through the list and look for all the non-empty edit texts.

adapter

public class CursorAdapter_EditText extends SimpleCursorAdapter {

private static Cursor c;
private Context context;
public static String[] quantity;
private int layout;

public CursorAdapter_EditText(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);
    CursorAdapter_EditText.c = c;
    this.context = context;
    this.layout = layout;
    initializeQuantity();     // set array to hold contents od list edit text boxes
}

public static void initializeQuantity() {
    quantity = new String[c.getCount()];
    int i = 0;
    while (i < c.getCount()) {
        quantity[i] = "0";     // set all array quantities to 0
        i++;
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = View.inflate(context, layout, null);
    final int pos = position;
    View row = convertView;
    c.moveToPosition(position);
    TextView name = (TextView) row.findViewById(R.id.ListItem1);
    TextView unit = (TextView) row.findViewById(R.id.ListItem2);
    EditText qty = (EditText) row.findViewById(R.id.qty);
    qty.setOnFocusChangeListener(new View.OnFocusChangeListener() {    //  save changes to the array when the user leaves an edit box
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                LinearLayout parent = (LinearLayout) v.getParent();
                EditText qtyTemp = (EditText) parent.findViewById(R.id.qty);
                quantity[pos] = qtyTemp.getText().toString();
            }
        }
    });
    name.setText(c.getString(1));
    unit.setText(c.getString(3));
    qty.setText(quantity[position]);  //  set the value in the edit box to the value stored in the array
    return (row);
}
}

Then I have a button in the layout that holds the listview to process the list once the user has edited the things they want to.

button code

    commit.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            int i = 0;
            itemCursor.moveToFirst();
            while (itemCursor.isAfterLast() == false) {
                if (CursorAdapter_EditText.quantity[i].equals("")) {
                    CursorAdapter_EditText.quantity[i] = "0";
                }
                ;
                int tempQty = Integer
                        .parseInt(CursorAdapter_EditText.quantity[i]);
                if (tempQty != 0) {
                    mDbHelper.createListItem(listId, itemCursor
                            .getInt(itemCursor
                                    .getColumnIndex(GroceryDB.ITEM_ROWID)),
                            tempQty, 0);
                }
                i++;
                itemCursor.moveToNext();
            }
            dismiss();
        }
    });

You could add an array to track checkbox state and then use that instead of a value in the edittext to run your if statement.

Barak
  • 16,318
  • 9
  • 52
  • 84
0

You may need to setItemsCanFocus to be able to access your view clicks in the row.

Check out this post Focusable EditText inside ListView

Community
  • 1
  • 1
Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64