Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know if I understood your question: If your app flow is: </p> <blockquote> <p>show a list of data(<code>CheckBox</code> + <code>TextView</code>(<code>Spinners</code> hidden)) -><br /> user clicks a row(the <code>Spinners</code> appear for that row with(individual) data) -><br /> user selects something in those <code>Spinners</code>-> save that selection in the database</p> </blockquote> <p>then I think you should go with a custom adapter and take care yourself of the row creation + data binding(I don't see how you would set a listener for the <code>Spinners</code>). Below is a small example on how you might do this(although probably not a pretty way of doing it):</p> <pre><code>public class CustomAdapter extends SimpleCursorAdapter { private LayoutInflater mInflater; public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); mInflater = LayoutInflater.from(context); } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder holder = (ViewHolder) view.getTag(); // the holder // pattern // set the text for the TextView in your row holder.name .setText(cursor.getString(cursor.getColumnIndex("name"))); // status of the CheckBox from the database int status = cursor.getInt(cursor.getColumnIndex("pl_selected")); // set the CheckBox status holder.ckb.setChecked((status &gt; 0) ? true : false); // get the id of this particular row, we'll use this later in the // Spinner's listeners long theId = cursor.getLong(cursor.getColumnIndex("_id")); // see if it is time to show the Spinners if (status &gt; 0) { // it is time to show the Spinners. Here you would do stuff // like: setting up the Spinner's adapters + setting the // listener // I used a Spinner with entries set in the xml layout(so my // selection result is a String) holder.spin1.setVisibility(View.VISIBLE); holder.spin2.setVisibility(View.VISIBLE); // set theId as a tag so you know which Spinner was acted on holder.spin1.setTag(new Long(theId)); holder.spin1 .setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) { Long realRowId = (Long) parent.getTag(); // I don't know ContentValues cv = new ContentValues(); // the column where I saved the spinner selected // item is called "saved_item" cv.put("saved_item", (String) parent .getItemAtPosition(position)); // mDb is my SQLiteDatabase instance mDb.update("tbl", cv, "_id = ?", new String[] { String .valueOf(realRowId) }); // I don't know how you saved the data, the // above is just an example } @Override public void onNothingSelected(AdapterView&lt;?&gt; parent) { } }); // also implement the second Spinner like the first one } else { // required to prevent a recycled View from causing damage holder.spin1.setVisibility(View.GONE); holder.spin2.setVisibility(View.GONE); } } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View v = mInflater.inflate(R.layout.adapters_listspinner_row, parent, false); ViewHolder holder = new ViewHolder(); holder.spin1 = (Spinner) v.findViewById(R.id.spinner1); holder.spin1.setFocusable(false); holder.spin2 = (Spinner) v.findViewById(R.id.spinner2); holder.spin2.setFocusable(false); holder.name = (TextView) v.findViewById(R.id.textView1); holder.ckb = (CheckBox) v.findViewById(R.id.checkBox1); holder.ckb.setFocusable(false); v.setTag(holder); return v; } class ViewHolder { Spinner spin1, spin2; TextView name; CheckBox ckb; } } </code></pre> <p>Also, the required <code>onListItemcClick</code> method:</p> <pre><code>@Override protected void onListItemClick(ListView l, View v, int position, long id) { // manage the CheckBox state CheckBox ckb = (CheckBox) v.findViewById(R.id.checkBox1); ckb.setChecked(!ckb.isChecked()); ContentValues cv = new ContentValues(); cv.put("pl_selected", ckb.isChecked() ? 1 : 0); mDb.update("tbl", cv, "_id = ?", new String[] { String.valueOf(id) }); // requery the database so the changes are seen by the adapter, this is horrible! Cursor re = mDb.query("tbl", null, null, null, null, null, null); mAllSs.changeCursor(re); } </code></pre> <p>As an advice, maybe you could modify the layout of your app and move the <code>Spinners</code> out of the <code>ListView</code> row.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload