我想要做的是看起来像添加到购物车的东西,但在我的Android应用程序上,首先我有添加到购物车活动,其具有我需要的所有值,当我按下按钮我发送我的数据作为额外的 cartList 活动,它显示在行上,每次我按下某个项目上的按钮,它清除数组,只显示一行!但我真正需要做的是每当我按下按钮我希望它添加到以前的项目并显示所有..这是我的添加到购物车活动:
public class AddToCart extends Activity{
EditText quantity=null;
TextView total=null;
TextView name=null;
TextView price=null;
TextView ava=null;
double totalprice=0.0;
ArrayList<String> listOfItemNames = new ArrayList<String>();
List<listClass>testList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
Intent test = getIntent();
Intent intent = getIntent();
final String itemprice = intent.getStringExtra("price");
final String itemname = intent.getStringExtra("item");
final String itemava = intent.getStringExtra("ava");
final int imagehandler=intent.getIntExtra("image", 0);
Log.e("image handler",imagehandler+"");
name=(TextView)findViewById(R.id.textView2);
price=(TextView)findViewById(R.id.textView4);
total=(TextView)findViewById(R.id.textView7);
ava=(TextView)findViewById(R.id.textView9);
quantity=(EditText)findViewById(R.id.editText1);
Button addtocart=(Button)findViewById(R.id.button1);
ImageView imageview=(ImageView)findViewById(R.id.imageView1);
name.setText(itemname);
price.setText(itemprice);
ava.setText(itemava);
imageview.setImageResource(imagehandler);
addtocart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String quantityvalue=quantity.getText().toString();
CharSequence currentavaiable=ava.getText();
int currentava=Integer.parseInt((String) currentavaiable);
Editable checked = quantity.getText();
String checkquantity=checked.toString();
Log.e("checked ",checked+"");
Log.e("check quantity",checkquantity);
try{
if(checkquantity==null){
Toast.makeText(AddToCart.this,"please enter quantity for your item", Toast.LENGTH_LONG).show();
}
else{
int x=0;
double y=0.0;
// String xvalue=quantity.getText().toString();
x = Integer.parseInt(quantityvalue);
Log.e("x",x+"");
y = Double.parseDouble(itemprice);
Log.e("x",x+"");
Log.e("y",y+"");
totalprice=x*y;
total.setText(totalprice+"");
Toast.makeText(AddToCart.this,"your item added succesfully !", Toast.LENGTH_LONG).show();
}
Intent newintent=new Intent(AddToCart.this,CartList.class);
newintent.putExtra("itemname",itemname);
newintent.putExtra("itemprice",itemprice);
newintent.putExtra("itemquantity",quantityvalue);
newintent.putExtra("totalprice",totalprice);
newintent.putExtra("image", imagehandler);
startActivity(newintent);
}//view
catch(Exception e){
e.printStackTrace();
}
}
});
}
}
这是购物车清单类:
public class CartList extends Activity {
ListView list;
List<CartItems> detailsList = new ArrayList<CartItems>();
ArrayList<String> listtest=new ArrayList <String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.cart_list);
initializeComponents();
detailsList = SetAdapterList();
CustomAdapter adapter=new CustomAdapter(this,detailsList);
list.setAdapter(adapter);
}
private void initializeComponents()
{
list=(ListView)findViewById(R.id.listView1);
}
private List<CartItems> SetAdapterList()
{
// TODO Auto-generated method stub
CartItems details;
List<CartItems> detailsList = new ArrayList<CartItems>();
try{
Intent newintent = getIntent();
final String itemname = newintent.getStringExtra("itemname");
final String itemprice = newintent.getStringExtra("itemprice");
final String itemquantity = newintent.getStringExtra("itemquantity");
final double totalprice = newintent.getDoubleExtra("totalprice", 0.0);
String totprice=String.valueOf(totalprice);
// ArrayList <String> itemnames=new ArrayList<String>();
listtest.add(itemname);
Log.e("listtest",listtest+"");
final int imagehandler=newintent.getIntExtra("image", 0);
Log.e("image handler",imagehandler+"");
for(int i = 0; i< listtest.size(); i++)
{
details = new CartItems(itemname,itemprice,itemquantity,totprice,imagehandler);
detailsList.add(details);
}
}
catch(Exception e){
e.printStackTrace();
}
return detailsList;
}
}
这是我的自定义适配器:
public class CustomAdapter extends ArrayAdapter<CartItems> {
Context context;
List<CartItems>Clist = new ArrayList<CartItems>();
public CustomAdapter(Context context, List<CartItems> detailsList) {
super(context, R.layout.single_row, R.id.textView1, detailsList);
// TODO Auto-generated constructor stub
this.context=context;
this.Clist = detailsList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row, parent,false);
ImageView myimage=(ImageView) row.findViewById(R.id.imageView1);
TextView items=(TextView)row.findViewById(R.id.textView1);
TextView totalprice=(TextView)row.findViewById(R.id.textView2);
items.setText(Clist.get(position).getItemName());
totalprice.setText(Clist.get(position).getTotalPrice());
myimage.setImageResource(Clist.get(position).getImage());
return row;
}
}
这是我的itemcart类:
public class CartItems {
private String itemName;
private String itemPrice;
private String Quantity;
private String totalPrice;
private int image;
public CartItems(String itemname,String itemprice, String quantity, String totalprice2, int image){
this.itemName=itemname;
this.itemPrice=itemprice;
this.Quantity=quantity;
this.totalPrice=totalprice2;
this.image=image;
}
//getter and setters
}
请有人帮帮我吗?我很绝望!!
答案 0 :(得分:2)
你正在重新设置arraylist everyime。相反,每次选择项目时都会向其添加项目。请按照以下步骤操作:
1)将你的setadapter函数改为return item,而不是itemlist。
private CartItems SetAdapterList()
{
// TODO Auto-generated method stub
CartItems details;
try{
Intent newintent = getIntent();
final String itemname = newintent.getStringExtra("itemname");
final String itemprice = newintent.getStringExtra("itemprice");
final String itemquantity = newintent.getStringExtra("itemquantity");
final double totalprice = newintent.getDoubleExtra("totalprice", 0.0);
String totprice=String.valueOf(totalprice);
// ArrayList <String> itemnames=new ArrayList<String>();
listtest.add(itemname);
Log.e("listtest",listtest+"");
final int imagehandler=newintent.getIntExtra("image", 0);
Log.e("image handler",imagehandler+"");
for(int i = 0; i< listtest.size(); i++)
{
details = new CartItems(itemname,itemprice,itemquantity,totprice,imagehandler);
}
}
catch(Exception e){
e.printStackTrace();
}
return details;
}
2)获得此值后,只需将其添加到您的arraylist。像这样:
CartItems item = SetAdapterList();
detailsList.add(item);
现在,当您填充listView时,detailsList也将包含所有先前选择的项目。当你想重置时,只需清除detailsList。
答案 1 :(得分:1)
1)将新项目添加到您的detailsList。
2)为适配器调用notifyDataSetChanged()
。