我不能讓我的ArrayList
當它在不同的頁面上。我想從Android Studio中獲得ArrayList
。如何獲取陣列列表Android工作室
這是CartActivity
類:
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);
}
}
這是ProductActivity
類。這是該數據被存儲到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);
}
爲什麼你會期望從粥活動訪問一般來說,一項活動中的變量可以從另一項活動中獲得? –
您應該將數組列表從活動A傳遞給B,然後您可以將新項目添加到該項目中,請參閱以下參考:https://stackoverflow.com/a/21250407/4146722 –
不是一個活動中的變量可訪問從另一個活動? – KUROYUKI