如何从实现ListAdapter的适配器刷新List

时间:2013-06-27 09:49:26

标签: android listadapter

我有一个实现ListAdapter的适配器,

我在列表项目上有一个按钮,当我点击该按钮时,该项目将从数据中删除,从列表中删除该项目后我想刷新列表,

ListAdapter

中没有

notifyDataSetChanged()方法

已编辑:

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;
}
}

4 个答案:

答案 0 :(得分:2)

而不是ListAdapter使用Adapter扩展您的BaseAdapter。然后你可以使用notifyDataSetChanged


修改 请参阅下文。

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

答案 1 :(得分:0)

尝试使用

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

而不是public class ShoppingListAdapter implements ListAdapter{}。如果有错误,请告诉我们错误说的是什么。

答案 2 :(得分:0)

试试这段代码..

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                              

    }
});

答案 3 :(得分:0)

该特定方法调用DataSetObserver。您可以非常轻松地重新创建相同的方法。您应该参考BaseAdapter的源代码以查看ListAdapter接口的实现。你可以在grepcode找到它。您还应该在代码中包含一个add(object)方法,以使其更具响应性和可重用性。

实施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();
    }

添加添加方法

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

真正有趣的是开始使它成为一个可过滤的真正适配器,您应该查看ArrayAdapter的grepcode并查看所涉及的所有内容。关于android的最好的事情之一是开源。所以当有疑问时请阅读。变量是自我解释的,并且注释是一流的。