Note that there are some explanatory texts on larger screens.

plurals
  1. POnotifyDataSetChanged in Curser adapter not working with listfragment
    primarykey
    data
    text
    <p>I have a listfragment that displays a list from a database. I have everthing working correctly except for a small item that I can't seeem to clear up. The list view looks something like this.</p> <pre><code>----------------------------------------------------- text text text | some more text | delete row button | ----------------------------------------------------- </code></pre> <p>My problem: when a user presses the delete row button the row is deleted from the database but will not be removed from the screen until the activity is stopped and started again.</p> <p>I know that <code>notifyDataSetChanged()</code> is the preferred method for updating these lists but it does not seem to do anything... Anyhow, below is my code for the cursor adapter where <code>notifyDataSetChanged()</code> is called.</p> <pre><code>public class CalcCursorAdapter extends SimpleCursorAdapter{ private Context mContext; private ListView mListView; private int mLayout; private Cursor mcursor; protected static class ViewHolder { protected TextView text; protected ImageButton button; private int position; } @SuppressWarnings("deprecation") public CalcCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.mContext = context; this.mLayout = layout; this.mcursor = c; //mListView = .getListView(); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView summary = (TextView)view.findViewById(R.id.calctvPrice); TextView savings = (TextView)view.findViewById(R.id.calctvSavings); TextView percentOff = (TextView)view.findViewById(R.id.calctvpercentOff); summary.setText(cursor.getString(cursor.getColumnIndexOrThrow("qcFinalPrice"))); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { ViewHolder holder = new ViewHolder(); LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.calc_list_item, parent, false); holder.button = (ImageButton) v.findViewById(R.id.qcbtnDelete); holder.button.setOnClickListener(deleteButton); holder.position = cursor.getInt(cursor.getColumnIndexOrThrow("_id")); bindView(v, context, cursor); v.setTag(holder); return v; } private OnClickListener deleteButton = new OnClickListener() { @SuppressWarnings("deprecation") public void onClick(View v){ View view = (View) v.getParent(); ViewHolder holder = (ViewHolder) view.getTag(); int position = holder.position; DbHelper mDbHelper; mDbHelper = new DbHelper(mContext); mDbHelper.open(); mDbHelper.deleteCalc(position); mDbHelper.close(); String test = Integer.toString(position); Toast.makeText(mContext.getApplicationContext(), test, Toast.LENGTH_SHORT).show(); notifyDataSetChanged(); //ListView list = getListView(); } }; public long qcItemId(int position) { return position; } } </code></pre> <p>Thank you for your help.</p> <p>UPDATE:</p> <p>New button Code within my adapter:</p> <pre><code>private OnClickListener deleteButton = new OnClickListener() { @SuppressWarnings("deprecation") public void onClick(View v){ v.invalidate(); View view = (View) v.getParent(); view.invalidate(); ViewHolder holder = (ViewHolder) view.getTag(); int position = holder.position; DbHelper mDbHelper; mDbHelper = new DbHelper(mContext); mDbHelper.open(); mDbHelper.deleteCalc(position); mDbHelper.close(); String test = Integer.toString(position); Toast.makeText(mContext.getApplicationContext(), test, Toast.LENGTH_SHORT).show(); Cursor newCursor = getCursor(); changeCursor(newCursor); notifyDataSetChanged(); //ListView list = getListView(); } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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