Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am unable to get my ArrayList out when it's on a different page. I would like to get the ArrayList out Android Studio.

This is CartActivity class:

public class CartActivity extends AppCompatActivity {
    private ArrayList<CartItem> cartList;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        listView = (ListView) findViewById(R.id.listView);
        CartAdapter adapter = null;
        cartList = new ArrayList<>();
        adapter = new CartAdapter(this, R.layout.adapter_cart_item, cartList);
        listView.setAdapter(adapter);
    }
}

This is ProductActivity class. It's where the data is being stored into the ArrayList:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product);
    myDB = new DatabaseHelper(ProductActivity.this);
    textViewPrice = (TextView) findViewById(R.id.textViewPrice);
    textViewInfo = (TextView) findViewById(R.id.textViewInfo);
    textViewName = (TextView) findViewById(R.id.textViewName);
    ivProduct = (ImageView) findViewById(R.id.ivProduct);
    btnAddToCart = (Button) findViewById(R.id.btnAddToCart);
    spinnerQuantity = (Spinner) findViewById(R.id.spinnerQuantity);


    Intent in = getIntent();
    Bundle b = in.getExtras();
    userID = b.getString("userID");
    productID = b.getInt("productID");
    Cursor results = myDB.checkProduct();
    if (results.getCount() == 0) {
        Toast.makeText(getApplicationContext(), "Error: no data found!",
                Toast.LENGTH_SHORT).show();
        return;
    } else {
        StringBuffer buffer = new StringBuffer();
        while (results.moveToNext()) {
            id = results.getInt(0);
            name = results.getString(1);
            price = results.getString(2);
            info = results.getString(4);
            quantity = Integer.parseInt(results.getString(5));
            image = results.getBlob(6);
            if (id == productID) {
                textViewName.setText(name);
                textViewInfo.setText(info);
                textViewPrice.setText("S$" + price);

                int[] quantityValues = new int[quantity + 1];
                int counter = 0;
                for (int i = 0; i < quantityValues.length; i++) {
                    quantityValues[i] = counter;
                    counter++;
                    quantityList.add(Integer.toString(quantityValues[i]));

                }
                Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
                ivProduct.setImageBitmap(bitmap);


                ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, quantityList);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                spinnerQuantity.setAdapter(adapter);
            }
        }
    }
}

public void addCart(View v) {
    cartList.add(new CartItem(name, price,spinnerQuantity.getSelectedItem().toString(), image, id));

    Intent productListIntent = new Intent(this, CartActivity.class);
    startActivity(productListIntent);

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
190 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should impliment Serializable in CartItem class and then pass from ProductActivity to CartActivity . Like this :

ProductActivity class.

Intent productListIntent= new Intent(this, CartActivity .class);
  productListIntent.putExtra("cartlist", ArrayList<CartItem>mcartItem);
startActivity(productListIntent);

and in CartActivity class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
      ArrayList<CartItem> mycart= new ArrayList<CartItem>();
          mycart= (ArrayList<CartItem>)getIntent().getSerializableExtra("cartlist");
     listView = (ListView) findViewById(R.id.listView);
        CartAdapter adapter = null;
        adapter = new CartAdapter(this, R.layout.adapter_cart_item, mycart);
        listView.setAdapter(adapter);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...