views:

116

answers:

1

Hi,

I created a ListView that contains a row which in turn contain text and a button. The idea is to have the button function as a delete button to remove the row from the list as well as the database.

I order to do this I created an adapter to handle the button click. This code is below. Deleting the database record works fine, but I have not yet succeeded in refreshing the ListView so the record will no longer be displayed.

public class FeedArrayAdapter extends ArrayAdapter {

private ARssEReaderDBAdapter dba;
private String TAG = "FeedArrayAdapter";
private View v;
private ListView feedList;

private OnClickListener btnDeleteFeedListener = new OnClickListener(){

    public void onClick(View pView) {
        dba = new ARssEReaderDBAdapter(getContext());

        Integer objInt = (Integer) pView.getTag();
        dba.open();
        dba.deleteFeed(objInt);
        dba.close();

        Log.w(TAG, "Database row, " + pView.getTag() + " got clicked.");
    }
};

public FeedArrayAdapter(Context context, int textViewResourceId, List<Feed> items) {
    super(context, textViewResourceId, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    Log.w(TAG, "getView");
    v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.feedlistrow, null);
    }
    Feed feed = getItem(position);
    if (feed != null){
        v.setTag(feed.getFeedId());
        TextView title = (TextView)v.findViewById(R.id.TextView01);
        if (title != null){
            title.setText(feed.getTitle());
        }
        Button btnDelete = (Button)v.findViewById(R.id.btnDelete);
        btnDelete.setTag(feed.getFeedId());
        btnDelete.setOnClickListener(btnDeleteFeedListener); //
    }
    return v;
}

}

Any help is greatly appreciated.

Richard

A: 

Perhaps you're looking for notifyDataSetChanged(). Make sure to call it from the UI thread.

public void notifyDataSetChanged ()

Since: API Level 1
Notifies the attached View that the underlying data has been changed and it should refresh itself.
Justin
The array adapter has a method setNotifyOnChange which is default set to true. Calling notifyDataSetChanged should only have effect if it was manually set to false.Needless to say I have not gotten this to work. I seem to be unable to call it from the click handler. Calling it from other places did not have any effect.
Richard
That's correct, `setNotifyOnChange()` removes the need to call this method directly. I did not see that in the code snippet you posted.
Justin
I called it on the adapter right after initialization in the list activity, but the default is set to true so I removed it since it didn't seem to have any effect.
Richard