我希望有人能帮助我,因为我现在完全迷路了。
我制作了这个从给定列表填充列表视图的自定义适配器。
问题在于减号和加号按钮。他们都有侦听器,可以根据按下哪个来修改文本视图,计数为 1,它还修改了源列表。
这是说明最终视图的图片PIC 。
如果从第一行开始按下的按钮一切正常,那么您可以使用其余的按钮来解决问题。但是,如果按下的是其他之一,则应用程序崩溃。
尝试在特定列表项的原始列表中添加+1 或remove-1 时,给出的错误是空指针异常。
如果还需要什么,请询问。感谢您的关注。
公共类 MenuListAdapter 扩展 BaseAdapter {
private Context mContext;
private int mLayoutResourceId;
public MenuListAdapter(Context context, int textViewResourceId) {
mContext = context;
mLayoutResourceId = textViewResourceId;
}
public int getCount() {
return SavedMenuList.INSTANCE.size();
}
public MenuListItem getItem(int position) {
return SavedMenuList.INSTANCE.getItem(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
rowView = inflater.inflate(mLayoutResourceId, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox) rowView.findViewById(R.id.confirmation_list_row_check);
viewHolder.text = (TextView) rowView.findViewById(R.id.confirmation_list_row_name);
viewHolder.minus = (ImageButton) rowView.findViewById(R.id.confirmation_list_row_remove_button);
viewHolder.minus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MenuListItem item = (MenuListItem) viewHolder.minus.getTag();
SavedMenuList.INSTANCE.removeOneItemCount(item);
notifyDataSetChanged();
}
});
viewHolder.count = (TextView) rowView.findViewById(R.id.confirmation_list_row_count);
viewHolder.plus = (ImageButton) rowView.findViewById(R.id.confirmation_list_row_add_button);
viewHolder.plus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MenuListItem item = (MenuListItem) viewHolder.plus.getTag();
SavedMenuList.INSTANCE.addOneItemCount(item);
notifyDataSetChanged();
}
});
viewHolder.itemid = (TextView) rowView.findViewById(R.id.confirmation_list_row_itemid);
viewHolder.typeid = (TextView) rowView.findViewById(R.id.confirmation_list_row_typeid);
rowView.setTag(viewHolder);
}else{
rowView = convertView;
((ViewHolder) rowView.getTag()).checkBox.setTag(getItem(position));
((ViewHolder) rowView.getTag()).minus.setTag(getItem(position));
((ViewHolder) rowView.getTag()).plus.setTag(getItem(position));
}
ViewHolder holder = (ViewHolder) rowView.getTag();
MenuListItem item = getItem(position);
holder.text.setText(item.getmItemName() + " (" + item.getmItemTypeName() + ")");
holder.count.setText(String.valueOf(item.getmItemCount()));
holder.typeid.setText(String.valueOf(item.getmItemId()));
holder.typeid.setText(String.valueOf(item.getmItemTypeId()));
return rowView;
}
static class ViewHolder {
public CheckBox checkBox;
public TextView text;
public ImageButton minus;
public TextView count;
public ImageButton plus;
public TextView itemid;
public TextView typeid;
}
}
这是错误:
04-22 02:54:22.398: E/AndroidRuntime(7112): 在 giorgi.betaproject.utils.SavedMenuList.addOneItemCount(SavedMenuList.java:34)
public enum SavedMenuList {
INSTANCE;
List <MenuListItem> mList = new ArrayList<MenuListItem> ();
...
public boolean addOneItemCount(MenuListItem item) {
for (MenuListItem mItem: mList){
if (item.equals(mItem)){
mItem.addOneItemCount();
return true;
}
}
return false;
}
...
}