尝试在Xamarin.Android中开发物品订购系统。为了显示项目列表,我在每行中都使用了带有自定义适配器的listView和Cart按钮。你能否告诉我应该使用哪种技术,或者如何将每个订购的商品存储在我的购物车列表活动列表中? 这是我的itemListAdapter类:
谢谢!
namespace Test.Restaurant.Android.Adapters
{
public class ItemListAdapter : BaseAdapter
{
List mItems;
Activity context;
private OrderDetail _OrderDetails;
private List _OrderDetailsList;
public ItemListAdapter(Activity context, List<Item> items) : base()
{
this.context = context;
this.mItems = items;
}
public override long GetItemId(int position)
{
return position;
}
public override Item this[int position]
{
get { return mItems[position]; }
}
public override int Count
{
get { return mItems.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView ?? context.LayoutInflater.Inflate(Resource.Layout.ItemRowView, parent, false);
var viewHolder = view.Tag as ItemHolder;
var item = mItems[position];
Bitmap myImage = ImageHelper.Bytes2Bimap(item.Image);
// 03. List With Custom Row View
if (viewHolder == null)
{
viewHolder = new ItemHolder
{
itemNameTextView = view.FindViewById<TextView>(Resource.Id.itemNameTextView),
descriptionTextView = view.FindViewById<TextView>(Resource.Id.descriptionTextView),
itemImageView = view.FindViewById<ImageView>(Resource.Id.itemImageView),
priceTextView = view.FindViewById<TextView>(Resource.Id.priceTextView),
btnPlus = view.FindViewById<ImageView>(Resource.Id.btnPlus),
btnMinus = view.FindViewById<ImageView>(Resource.Id.btnMinus),
btnAddtoCart = view.FindViewById<ImageView>(Resource.Id.btnAddToCart),
txtQty = view.FindViewById<TextView>(Resource.Id.cart_product_quantity)
};
view.Tag = viewHolder;
}
else
{
viewHolder = view.Tag as ItemHolder;
}
viewHolder.itemNameTextView.Text = item.ItemName;
viewHolder.descriptionTextView.Text = item.ItemDescription;
viewHolder.priceTextView.Text = "$ " + 30;
viewHolder.itemImageView.SetImageBitmap(myImage);
if (!viewHolder.btnAddtoCart.HasOnClickListeners)
{
viewHolder.btnAddtoCart.Click += (sender, args) =>
{
_OrderDetails = new OrderDetail();
_OrderDetails.ItemId = item.ItemId;
_OrderDetails.Quantity = int.Parse(viewHolder.txtQty.Text);
_OrderDetails.UnitPriceInclTax = 0;
_OrderDetails.UnitPriceExclTax = 0;
_OrderDetails.DiscountAmountInclTax = 0;
_OrderDetails.DiscountAmountExclTax = 0;
_OrderDetails.PriceInclTax = 100;
_OrderDetails.PriceExclTax = 0;
** //What should I Do Here???**
**************************
**************************
};
}
return view;
}
private class ItemHolder : Java.Lang.Object
{
public TextView itemNameTextView { get; set; }
public TextView descriptionTextView { get; set; }
public TextView txtQty { get; set; }
public ImageView itemImageView { get; set; }
public ImageView btnPlus { get; set; }
public ImageView btnMinus { get; set; }
public ImageView btnAddtoCart { get; set; }
}
}
}
答案 0 :(得分:0)
您能告诉我应该使用哪种技术,或者如何将列出的所有订购商品存储到我的购物车列表活动中?
您需要将CartLilst(List<OrderDetail>
)传递给适配器:
public class ItemListAdapter : BaseAdapter
{
...
List<OrderDetail> cartList;
public ItemListAdapter(Activity context, List<Item> items, List<OrderDetail> cartList)
{
this.context = context;
this.mItems = items;
_cartlist = cartList;
}
...
}
当用户点击addToCart按钮时,将OrderDetail添加到此列表中:
viewHolder.btnAddtoCart.Click += (sender, args) =>
{
_OrderDetails = new OrderDetail();
_OrderDetails.ItemId = item.ItemId;
_OrderDetails.Quantity = int.Parse(viewHolder.txtQty.Text);
_OrderDetails.UnitPriceInclTax = 0;
_OrderDetails.UnitPriceExclTax = 0;
_OrderDetails.DiscountAmountInclTax = 0;
_OrderDetails.DiscountAmountExclTax = 0;
_OrderDetails.PriceInclTax = 100;
_OrderDetails.PriceExclTax = 0;
//add _OrderDetail to cartList
cartList.Add(_OrderDetails);
};
应该在适配器创建之前的Activity中初始化并创建列表:
List<OrderDetail> cartList=new List<OrderDetail>();
ItemListAdapter adapter=new ItemListAdapter(this,items,cartList);
<强>更新强>
如果您的购物车清单在您的应用程序中经常使用,那么我建议您忘记我所说的内容,并使用单一模式将购物车列表存储在您的应用中:
在您的应用中创建CartList.cs
:
public class CartList
{
private static List<OrderDetail> _cartList;
public static List<OrderDetail> Instance
{
get
{
if (_cartList == null)
{
_cartList = new List<OrderDetail>();
}
return _cartList;
}
set
{
_cartList = value;
}
}
}
然后你可以在你的应用程序的任何地方使用这个CartList(你不需要将它传递给你的新Activity,你可以直接在你的新Activity中获取它):
var cartList=CartList.Instance;