我以以下方式做了类似的事情(用作对话框)。我不使用复选框,我只是处理列表并查找所有非空编辑文本。
适配器
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);
}
}
然后,我在布局中有一个按钮,用于保存列表视图,以便在用户编辑他们想要的内容后处理列表。
按钮代码
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();
}
});
您可以添加一个数组来跟踪复选框状态,然后使用它而不是 edittext 中的值来运行您的if
语句。