In android I have a custom Adapter, in this Custom Adapter I create a view with some text and a button to delete the text. All of this works fine and I can delete the record from the database but I have been unable to refresh the list.
The list looks a little like this:
------------------------------------
text text text text | Delete Button|
------------------------------------
text text text text | Delete Button|
------------------------------------
My first instinct was to use something like this:
QCListFragment frag = (QCListFragment) getSupportFragmentManager().findFragmentById(R.id.listFragment);
At what point I could call the method inside my list fragment to re-query the list... But I cant do this because my adapter does not extend Fragment. So I'm finding it hard to let the fragment know that the data has been changed.
Anyhow, my last thought was just to rebuild the activity but I thought that that was probably not the best thing to do and that there had to be a better way... I'm sure it is something simple that I'm missing like my last question was!
(I can post more code if it would be helpful) UPDATE: Here my entire adapter class
public class CalcCursorAdapter extends SimpleCursorAdapter implements Filterable{
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);
summary.setText(cursor.getString(cursor.getColumnIndexOrThrow("qcFinalPrice")));
savings.setText(cursor.getString(cursor.getColumnIndexOrThrow("qcSavings")));
}
@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() {
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();
}
};
public long qcItemId(int position) {
return position;
}
}
any help is much appreciated!
Thanks.