我有一个实现 ListAdapter 的适配器,
我在列表项目上有一个按钮,当我单击该按钮时,该项目将从数据中删除,从列表中删除该项目后,我想刷新列表,
notifyDataSetChanged() 方法在 ListAdapter 中不可用
编辑:
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;
}
}