我正在编写购物车应用程序,其中我需要允许用户在购物车中添加多个商品,就像我们在其他购物车应用中看到的一样。
在这里,我编写了一个代码,用于在购物车中添加商品,但不允许用户在购物车中添加多个商品。
而且我也不想在列表中显示重复的项目
Constants.java: -
public class Constants {
public static ArrayList<HashMap<String, String>> sItem_Detail = new ArrayList<HashMap<String, String>>();
}
LazyAdapter.java: -
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
String itemTitle, itemCost;
TextView title, cost;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.listrow_items, null);
title = (TextView)vi.findViewById(R.id.title);
cost = (TextView)vi.findViewById(R.id.cost);
ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> item = new HashMap<String, String>();
item = data.get(position);
// Setting all values in listview
title.setText(item.get(ItemsActivity.KEY_TITLE));
cost.setText(item.get(ItemsActivity.KEY_COST));
imageLoader.DisplayImage(item.get(com.example.sample.ItemsActivity.KEY_THUMB_URL), thumb_image);
//Button mImgAddCart
Button mImgAddCart = (Button) vi.findViewById(R.id.btn_add_to_order);
mImgAddCart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
itemTitle = title.getText().toString();
itemCost = cost.getText().toString();
if (Constants.sItem_Detail.size() <= 0) {
HashMap<String, String> sTempObj = new HashMap<String, String>();
sTempObj.put(ItemsActivity.KEY_TITLE, itemTitle);
sTempObj.put(ItemsActivity.KEY_COST, itemCost);
Constants.sItem_Detail.add(sTempObj);
}
AlertDialog.Builder alertdialog = new AlertDialog.Builder(
activity);
alertdialog.setTitle(activity.getResources()
.getString(R.string.app_name));
alertdialog.setMessage("Item Added to Cart");
alertdialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which)
{
activity.finish();
}
});
alertdialog.show();
}
});
return vi;
}
}
请告诉我代码,我需要编写以实现此目的......
注意: 我仍然只获得列表中的单个项目