我正在尝试制作一个android服务员订购菜单,我可以通过单击菜单中产品中的添加/减去按钮来接受订单,并在单击另一个按钮时呈现订单摘要。我创建了一个自定义listivew和arrayAdapters。但是我被困在了我应该能够在我的列表视图中收集listitem的内容的部分,我点击了添加按钮。这是我的代码。我希望能够获取名称价格和我添加值的所有项目的数量。
public class coffee {
public String name;
public double price;
public long itemID;
public int qty;
public int Icon;
public coffee (String name, double price, int qty, int Icon){
this.name = name;
this.price = price;
this.qty = qty;
this.itemID = 0;
this.Icon = Icon;
}
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public int getQty(){
return qty;
}
public long getItemID(){
return itemID;
}
public int getIcon(){
return Icon;
}
public String toString(){
return "ID: " + itemID + "Name: " + name + "Price: " + price + "Quantity: " + qty + "Image: "+ Icon; }}
ArrayAdapter=咖啡适配器.java
public class coffee_adapter extends ArrayAdapter<coffee> {
public static class ViewHolder{
TextView itemName;
TextView itemPrice;
TextView itemQty;
ImageView itemImage;
Button addButton;
Button minusButton;
}
public coffee_adapter(Context context, ArrayList<coffee> cof) {
super(context, 0, cof);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final coffee coffeeList = getItem(position);
final ViewHolder viewHolder;
if(convertView == null ){
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_row_layout,parent, false);
viewHolder.itemImage = (ImageView) convertView.findViewById(R.id.itemImage);
viewHolder.itemName = (TextView) convertView.findViewById(R.id.itemName);
viewHolder.itemQty = (TextView) convertView.findViewById(R.id.quantity);
viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.itemPrice);
viewHolder.addButton = (Button) convertView.findViewById(R.id.plusButton);
viewHolder.minusButton = (Button) convertView.findViewById(R.id.minusButton);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.itemName.setText(coffeeList.getName());
viewHolder.itemPrice.setText(String.valueOf(coffeeList.getPrice()));
viewHolder.itemQty.setText(String.valueOf(coffeeList.getQty()));
viewHolder.addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
coffeeList.qty ++;
viewHolder.itemQty.setText(String.valueOf(coffeeList.qty));
Toast.makeText(getContext(), "WTF " +coffeeList.qty , Toast.LENGTH_SHORT).show();
}
});
viewHolder.minusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
coffeeList.qty --;
if(coffeeList.qty < 0){
coffeeList.qty = 0;
}
viewHolder.itemQty.setText(String.valueOf(coffeeList.qty));
}
});
return convertView;
}
}
ListFragment
public class coffee_list extends ListFragment {
private ArrayList<coffee> coffeeList;
private coffee_adapter coffeeAdapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
coffeeList = new ArrayList<coffee>();
coffeeList.add(new coffee("Qaldi Black(Americano)",75.00,0,R.drawable.testicon));
coffeeAdapter = new coffee_adapter(getActivity(), coffeeList);
setListAdapter(coffeeAdapter);
getListView().setDivider(ContextCompat.getDrawable(getActivity(), android.R.color.darker_gray));
getListView().setDividerHeight(1);
}
}
您需要调用< code > notifyDataSetChanged()来查看通过单击按钮所做的更改。
所以像这样:
viewHolder.itemQty.setText(String.valueOf(coffeeList.qty));
notifyDataSetChanged();
从你的评论中添加,如果你想重置列表,你可以在你的列表片段中尝试:
private ArrayList<coffee> baseCoffeeList;
//...initialize baseCoffeeList like you did before in onActivityCreated
public void resetList() {
setListAdapter(new coffee_adapter(getActivity(), baseCoffeeList));
}
或者(这个我没试过):
在<code>coffee_apter.java</code>中:
public class coffee_adapter extends ArrayAdapter<coffee> {
private ArrayList<coffee> coffeeList;
public coffee_adapter(Context context, ArrayList<coffee> cof) {
super(context, 0, cof);
this.coffeeList = cof;
}
public reset(ArrayList<coffee> baseCoffeeList){
coffeeList.clear();
coffeeList.addAll(baseCoffeeList);
notifyDataSetChanged();
}
}