我通过listView中的jsonarray在我的Android应用程序中获取数据,当我点击listview项目时,它将打开另一个活动,其中包含该项目的详细信息,在此活动中我有一个按钮添加到购物车,当我点击这个按钮,产品在新的活动中进入购物车,但当我按下后退按钮添加新产品,再次点击添加到购物车按钮,我的第一个产品被第二个产品替换。我还为CartActivity添加了listView和listViewAdapter。
的 CartActivity
public class CartActivity extends AppCompatActivity implements View.OnClickListener {
String item_name;
String item_price;
String image;
ImageLoader imageLoader = new ImageLoader(this);
public int counter = 1;
public int sum = 0;
public int quantity;
ImageView ivAdd;
ImageView ivMinus;
TextView numberItem;
TextView txtTotalPrice;
Button btnAddCart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
setTitle("Add to cart");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
item_name = intent.getStringExtra("item_name");
item_price = intent.getStringExtra("item_price");
image = intent.getStringExtra("image");
sum = Integer.parseInt(item_price);
TextView itemName = (TextView) findViewById(R.id.tvName);
TextView itemPrice = (TextView) findViewById(R.id.tvPrice);
ImageView itemImage = (ImageView) findViewById(R.id.ivImage);
txtTotalPrice = (TextView) findViewById(R.id.tvTotalTag);
numberItem = (TextView) findViewById(R.id.tvNumberItem);
ivMinus = (ImageView) findViewById(R.id.ivMinus);
ivAdd = (ImageView) findViewById(R.id.ivAdd);
btnAddCart = (Button) findViewById(R.id.btnCart);
itemName.setText(item_name);
itemPrice.setText(item_price);
imageLoader.DisplayImage(image, itemImage);
txtTotalPrice.setText(item_price);
ivAdd.setOnClickListener(this);
ivMinus.setOnClickListener(this);
btnAddCart.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == btnAddCart) {
Bundle bundle=new Bundle();
bundle.putString("name", item_name);
bundle.putString("price", item_price);
bundle.putInt("quantity", counter);
bundle.putInt("total", sum);
Intent intent = new Intent(SouthCartActivity.this, CheckoutActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
}
的 CheckoutActivity
public class CheckoutActivity extends AppCompatActivity {
static ListView listView;
CheckoutAdapter adapter;
String item_name[];
String item_price[];
int item_quantity[];
int item_total[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
setTitle("Checkout");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Bundle bundle = getIntent().getExtras();
item_name = new String[]{bundle.getString("name")};
item_price = new String[]{bundle.getString("price")};
item_quantity = new int[]{bundle.getInt("quantity")};
item_total = new int[]{bundle.getInt("total")};
adapter = new CheckoutAdapter(this, item_name, item_price, item_quantity, item_total);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}