1

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.

Marek Counts
  • 126
  • 3
  • 11
  • did you try notifyDatasetChanged() method? – Varundroid May 21 '13 at 03:08
  • The fragment does not handle the click, the adapter does, this is because the button that is clicked is inside the list. and means (at least to the best of my knowledge) I can't call adapter.notifyDatasetChanged() because I'm inside the adapter... – Marek Counts May 21 '13 at 03:26
  • Take a look at my Answer and let me know if you are now able to call notifyDatasetChanged() method. We'll try our level best to help you out. – Varundroid May 21 '13 at 03:48
  • Thank you. I tried what you said, to just add the call... it did not throw any error, but it also did not update the list. it just does nothing. – Marek Counts May 21 '13 at 03:55
  • That might be because your data set hasn't been changed. Please post your adapter code and the code that deleted the item so i can take a look and see what's wrong. Cheers!! – Varundroid May 21 '13 at 04:18
  • The data has changed, if I get out of that activity or force it to reload the row is gone... I can get you the code to my db adapter if you like but I can assure you it is deleting the row. – Marek Counts May 21 '13 at 22:39
  • @MarekCounts was this ever resolved? I'm facing a similar issue – Dory Dec 09 '13 at 06:48

2 Answers2

0

I think you misunderstood the concept of Adapter and Fragment. You can set Adapter to Fragment or AdapterView in your Fragment. After you requery data, call Adapter.notifyDataSetChanged(), then your Fragment or AdapterView will be refreshed.

handrenliang
  • 1,047
  • 1
  • 10
  • 21
  • I don't have an Adapter object inside of my adapter... so I'm not sure how I would perform said operation. my onclick listener is inside of my adapter because it is a button that is in each row. – Marek Counts May 21 '13 at 03:23
  • you dont need an object of Adapter inside adapter, just call notifyDataSetChanged(). – Varundroid May 21 '13 at 03:42
  • I call notifyDataSetChanged() within my adapter inside the OnClickListener and it does not do anything. I have posted my code above. – Marek Counts May 21 '13 at 03:52
0

When you are inside adapter you can directly call notifyDatasetChanged(). You don't need an object of Adapter. I think you need to refresh you OOPS concepts. You adapter extends BaseAdapter or ArrayAdapter or SimpleAdapter whatever, they all have this method and you don't need an object of class itself to call its own methods.

EDIT

notifyDataSetChanged() didn't work for you because you are inflating the view everytime, which isn't a good thing in terms of performance. Check out this Answer. And onContentChanged() gets called after notifyDataSetChanged(). i am not sure how that worked for you. I highly recommend you to use notifyDataSetChanged() to make things work, don't go for work arounds.

Community
  • 1
  • 1
Varundroid
  • 9,135
  • 14
  • 63
  • 93