1

I have a Adapter which implements ListAdapter,

I have a one button on item of list, when I click on that button, that item is deleted from data, after deleting that item from list I want to refresh the List,

notifyDataSetChanged() method is not available in ListAdapter

Edited:

public class ShoppingListAdapter implements ListAdapter {

ArrayList<ProductInfoDTO> data;
private LayoutInflater inflater = null;
Context context;
Activity activity;
String TAG = getClass().getSimpleName();

DBQuery mDBQuery;

public ShoppingListAdapter(Context context, ArrayList<ProductInfoDTO> data) {

    this.context = context;
    this.data = data;
    activity = (Activity) context;
    inflater = ((Activity) context).getLayoutInflater();

    mDBQuery = new DBQuery(context);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {

    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {   

    MyCartItemView view = null;

    if (convertView == null) {
        view = new MyCartItemView(activity, data.get(position));
    } else {
        view = (MyCartItemView) convertView;
    }
    view.setPosition(position);

    Button delete;

    delete = (Button) view.findViewById(R.id.delete);

    delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(context, "delete clicked", Toast.LENGTH_SHORT)
                    .show();

            // delete from database
            mDBQuery.DeleteCartData(data.get(position).cart_id);

            // delete from data
            data.remove(data.get(position));

            // now I want to refresh the list here                              

        }
    });


    return view;
}

@Override
public int getViewTypeCount() {
    return data.size();
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isEmpty() {
    return false;
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {

}

@Override
public boolean areAllItemsEnabled() {
    return true;
}

@Override
public boolean isEnabled(int position) {
    return true;
}
}
Jayesh
  • 3,661
  • 10
  • 46
  • 76
  • 1
    Did you implement ListAdapter directly? You should extend one of the subclasses of [BaseAdapter](http://developer.android.com/reference/android/widget/BaseAdapter.html) instead. BaseAdapter comes with the notifyDataSetChanged() method you are looking for. – devconsole Jun 27 '13 at 09:53
  • Without code...take a look here if it is your case http://stackoverflow.com/questions/14859766/how-do-i-get-notifydatasetchanged-to-work-with-a-listadapter – AlexBcn Jun 27 '13 at 09:54
  • @AlexBcn: In the link that you are talking about, ScoreListAdapter extends ArrayAdapter and in My case implemetns ListAdapter – Jayesh Jun 27 '13 at 10:00
  • @Jayesh also you haven't posted the code where you set the adapter to your list in the activity! – 7bluephoenix Jun 27 '13 at 10:29

4 Answers4

2

Instead of using ListAdapter extend your Adapter with BaseAdapter. Then you get to use notifyDataSetChanged.


Edit: Refer below.

https://stackoverflow.com/a/13431786/1995735

Community
  • 1
  • 1
7bluephoenix
  • 958
  • 7
  • 18
0

Try use

public class ShoppingListAdapter extends BaseAdapter {
    // add unimplemented methods...
}

instead of public class ShoppingListAdapter implements ListAdapter{}. If there is error, let us know what does the error say.

Neoh
  • 15,906
  • 14
  • 66
  • 78
  • need a solution, not sugesstion – Jayesh Jun 27 '13 at 10:34
  • In the swipelistview demo, packageAdapter extends baseAdapter and works just fine. We need to understand where is your error if you need a solution. – Neoh Jun 27 '13 at 10:36
  • there is no error any where bro, have you not seen the code, I have just add one comment that // now I want to refresh the list here and I download the demo code for swipeListView from github and they implement ListAdapter – Jayesh Jun 27 '13 at 10:40
  • @Neoh he hasn't given the code where he sets the adapter to his list and expects **PRECISE** answers from us! – 7bluephoenix Jun 27 '13 at 10:45
  • Can you point me which file that defines the class ShoppingListAdapter? – Neoh Jun 27 '13 at 10:47
  • i just use SwipeListView.java class from there, and Adapter is not located there, I create my own adapter here, but main issue is that can we refresh the list from ListAdapter or not ? – Jayesh Jun 27 '13 at 10:52
  • 1
    There is a sample adapter class called PackageAdapter in that demo, if you learn to create your own class similar to that class then you can refresh the list. ListAdapter won't be able to refresh the list unless you implement everything just like a BaseAdapter. But why would you reinvent the wheel? – Neoh Jun 27 '13 at 10:57
0

Try this code..

delete.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        Toast.makeText(context, "delete clicked", Toast.LENGTH_SHORT)
                .show();

        // delete from database
        mDBQuery.DeleteCartData(data.get(position).cart_id);

        // delete from data
        data.remove(data.get(position));

    your_listview.invalidateViews(); //refresh the list here                              

    }
});
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • display error on that line, notifyDataSetChanged(); method is not applicable to ListAdapter.... :( – Jayesh Jun 27 '13 at 12:01
  • but I have not ListView object in Adapter, it is in Activity or Fragment, in my case ListView is in fragment, normally we do like this listview.setAdapter(customAdapter); from activity or fragment, but we have not listview object inside Adapter Class – Jayesh Jun 27 '13 at 13:24
0

That particular method calls a DataSetObserver. You can recreate the same method very easily. You should reference the Source code for BaseAdapter to see the implementation of ListAdapter Interface. you Can find it here at grepcode. you should also include an add(object) method in your code to make it more responsive and reusable.

implementing DataSetObserver

public class YourClass extends Something implements ListAdapter{
     private final DataSetObservable mObservable = new DataSetObservable();
     @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        mObservable.registerObserver(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        mObservable.unregisterObserver(observer);
    }
    public void notifyDataSetChanged(){
        mObservable.notifyChanged();
    }
    public void notifyDataSetInvalidated(){
        mObservable.notifyInvalidated();
    }

adding an add method

public void add(YourDataType object){ data.add(object);}

The real fun begins making it a real adapter that is filterable which you should look at the grepcode for ArrayAdapter and see everything that involves. One of the best things about android is the open source. So when in doubt read. The variables are self explanatory and the notes are top notch.

jmodine
  • 76
  • 5