我的 ArrayAdapter 类有问题。问题是该项目在冰箱项目列表中被删除,但它没有反映在 UI 中。在视图中,仅删除最后一项,无论在活动中删除了哪个项目。
Activity.java(重要部分):
m_adapter.remove(m_adapter.getItem(id));
m_adapter.notifyDataSetChanged();
RefrigeratorItemAdapter.java:
public class RefrigeratorItemAdapter extends ArrayAdapter<RefrigeratorItem> {
private List<RefrigeratorItem> refrigeratorItemList;
public RefrigeratorItemAdapter(Context context, int textViewResourceId,
List<RefrigeratorItem> list) {
super(context, textViewResourceId, list);
this.refrigeratorItemList = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.simple_list_item_1, null);
RefrigeratorItem i = (RefrigeratorItem) getItem(position);
System.out.println(i.toString());
if (i != null) {
TextView tt = (TextView) v.findViewById(R.id.text1);
tt.setText(i.getProductname());
}
}
return v;
}
public void remove(RefrigeratorItem object) {
super.remove(object);
this.refrigeratorItemList.remove(object);
System.out.println(Arrays.toString(refrigeratorItemList.toArray()));
}
}
谢谢!