2012-06-27 65 views
2

我有一個ListView是像可見: -如何獲取通過複選框檢查的Listview中的edittext值?

(複選框) TEXT [的EditText]

(複選框) TEXT [的EditText]

(複選框) TEXT [EditText]

...

我真正想要的是獲取複選框中選中的所有edittext的值。

我已經使用類MyItem獲取和設置值,類MyAdpater爲該活動設置適配器和項目佈局以及類MyView。佈局不允許onItemClick/onItemSelected方法工作,因爲佈局具有複選框和編輯文本,因此它不會作爲列表項目單擊。

如果需要,我會添加更多信息。

回答

2

當你與你的適配器建立你的ListView,創建和數組列表在它一個新的複選框查看每個元素。

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)); 

} 

使用的ArrayList調用

adapter.notifyDataSetChanged(); 
2

我已經做以如下方式(用作對話框)類似的東西后,檢查值。我不使用複選框,我只是通過列表處理並查找所有非空編輯文本。

適配器

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聲明。

相關問題