刷新列表我有一個實現ListAdapter一個適配器,如何從適配器用於實現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;
}
}
你是否直接實現了ListAdapter?您應該擴展其中一個[BaseAdapter](http://developer.android.com/reference/android/widget/BaseAdapter.html)的子類。 BaseAdapter附帶您正在查找的notifyDataSetChanged()方法。 – devconsole
沒有代碼...在這裏看看,如果它是你的情況http://stackoverflow.com/questions/14859766/how-do-i-get-notifydatasetchanged-to-work-with-a-listadapter – AlexBcn
@AlexBcn:在你正在討論的鏈接中,ScoreListAdapter擴展了ArrayAdapter,並且在我的情況下實現了ListAdapter –
Jayesh