-1

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);

}
KUROYUKI
  • 93
  • 3
  • 14

3 Answers3

1

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);
    }
PRIYA PARASHAR
  • 747
  • 4
  • 15
0

You have to pass your list of your data via intent(as Intent is a message passing Object) From ! activity to another activity. For that you have to make that class Parcable or Serializable . How to make a class Parcable in andriod studio read this answer. Then put thah class into the intent that you are passing to startActivity() method like

intent.putParcelableArrayListExtra("key",your_list);

or

 intent.putExtra("key",your_class)// incase of single object

then get it to the onCreate() method of second activity like

getIntent().getParcelableArrayListExtra("your_key")

or

getIntent().getParcelableExtra()//  incase of single object

Hope it will help your

Ajeet Choudhary
  • 1,751
  • 1
  • 11
  • 37
0

It's not the best practice, but you can make your arrayList

public static ArrayList list;

Then you can access it from ather activity using {class_name}.list

igor
  • 666
  • 1
  • 6
  • 25