Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SimpleCursorAdapter getView how to put previous value back
    primarykey
    data
    text
    <p>I have a simple <code>ListActivity</code> that uses the <code>SimpleCursorAdapter</code>. I allow users to change one of the values using an <code>EditText</code>. I perform simple validation to make sure that the number entered is less than 100. If the user entered value fails validation, I want to put the old value back.</p> <p>I've tried a few different ways. My current approach is to requery it out of the database, but this isn't working. I'm always getting the value associated with the last entry in the <code>ListActivity</code>, regardless of which one was actually changed. I noticed in <code>LogCat</code> that <code>onTextChanged</code> and <code>afterTextChanged</code> are firing multiple times for each row in the <code>ListActivity</code> and not just the one that changed.</p> <p>Here's the code:</p> <pre><code>public class MySimpleCursorAdapter extends SimpleCursorAdapter { Context lcontext; boolean changed; String lastval; private PortfolioData pfdata; public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); lcontext = context; } @Override public View getView(final int pos, View v, ViewGroup parent) { v = super.getView(pos, v, parent); final EditText et = (EditText) v.findViewById(R.id.classpercentage); final TextView tv = (TextView) v.findViewById(R.id._id); et.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { Log.d("TEST", "In afterTextChanged s=" + s.toString() + " " + tv.getText() + " POS = " + Integer.toString(pos)); lastval = tv.getText().toString(); if (changed == true) { String enteredValue = s.toString(); if (checkNullValues(enteredValue)) { if (Float.parseFloat(enteredValue.trim()) &gt; 100.0f) { AlertDialog.Builder builder = new AlertDialog.Builder( lcontext); builder.setMessage("Percentage Value should be Less than 100"); builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick( DialogInterface arg0, int arg1) { String sql = "select c.percentage as PERCENTAGE " + "from asset_classes c WHERE c._id = " + lastval + ";"; pfdata = new PortfolioData(lcontext); SQLiteDatabase db = pfdata.getReadableDatabase(); Cursor cursor = db.rawQuery(sql, null); if (cursor != null) { cursor.moveToFirst(); et.setText(cursor.getString(0)); } cursor.close(); pfdata.close(); } }); // End of the Alert if (changed == true) { builder.show(); } } } changed = false; } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // Log.d("TEST", "In beforeTextChanged start=" + // Integer.toString(start) +" count="+ Integer.toString(count) + // " after=" + Integer.toString(after) + " s=" + s + " " + tv); } public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("TEST", "In onTextChanged start=" + Integer.toString(start) + " count=" + Integer.toString(count) + " before=" + Integer.toString(before) + " s=" + s + " " + tv); changed = true; } }); return v; } } </code></pre> <p>I would really appreciate a fresh perspective on this. As always, thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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