I’ve done a Custom Listview with 3 columns:
Product / Selected (check box)/ Quantity.
The first column i’ve get from sqllite database. And second and third column, i’ll write into a database.
I’ve the problem that when i write into qunatity column the checkbox i’ve checked is unchecked, and it doesn’t work.
Another problem is when select edittext to write a quantity the checkbox selections changes.
//productos.java
Button btComanda = (Button) findViewById(R.id.btComanda);
btComanda.setOnClickListener(new OnClickListener() {
public void onClick (View arg0) {
Log.v("Escriu Comanda", "inici");
final ListView prodSpinner = (ListView) findViewById(R.id.list);
final CheckBox cbArticle = (CheckBox) findViewById(R.id.TextView02);
final TextView txtQuantitat = (TextView) findViewById(R.id.TextView03);
int count = 0;
Log.v("Escriu Comanda 2",String.valueOf(prodCursor.getCount())+ " " + txtQuantitat);
for(int i = 0; i < prodCursor.getCount(); i++)
{
Log.v("inserta 1", String.valueOf(Producte)+ " " + cbArticle.isChecked());
Producte = i;
String a = prodSpinner.getItemAtPosition(i).toString();
InsertaComanda();
});
recCatSpinner();
}
public void recProdSpinner() {
ListView prodSpinner = (ListView) findViewById(R.id.list);
prodCursor = recuperaProductos();
prodSpinner.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
prodAdapter = new ArrayAdapter<String>(this, R.layout.listview, R.id.TextView02);
prodAdapter.setDropDownViewResource (R.layout.listview_item_row);
prodSpinner.setAdapter(prodAdapter);
if (prodCursor.moveToFirst()) {
do {
prodAdapter.add(prodCursor.getString(1));
}
while (prodCursor.moveToNext());
if (db != null) {
db.close();
}
}
startManagingCursor(prodCursor);
prodCursor.close();
prodSpinner.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int pos, long id) {
}
});
}
The xml file:
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10.77" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TextView>
<CheckBox
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/TextView02"
android:focusable="false" />
<EditText
android:id="@+id/TextView03"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
</TableLayout>
</LinearLayout>
Read about how ListViews work and you will understand why its happening – the ListView is populating each row when it appears on the screen (so your values and states will change when you scroll)
so what you need to do is to save the state at the moment the user is changing it to somewhere safe – array / database etc.. and when you load it under your custom list adapter you need to change the state into the right one
so for your checkboxs you need to implement a listener and save its state when it is changing and under getView you need to set the specific CheckBox to its saved state .
the thing is EditTexts inside ListViews is pretty damn problematic but it is possible if you are stubborn enough.
hope it helps and tell me if you need any more help.