[英]Add values to ArrayList from different activity
我正在嘗試創建一個類似購物車的應用程序,使用它來訪問我的數據庫http://www.tutecentral.com/restful-api-for-android-part-2/
而且我一直堅持將產品添加到購物車中,到目前為止,我了解到所選產品將在一些教程中進入arraylist。 在下面的代碼中,我有兩個活動,一個是MaterialView(顯示材料的詳細信息,可以選擇添加到購物車),另一個是MaterialCart(顯示所選產品的列表)。
這是MaterialView中的代碼塊,用於將值發送到MaterialCart
ButtonAdd.setOnClickListener(new View.OnClickListener(){
public void onClick (View view){
Intent i=new Intent(MaterialView.this, MaterialCart.class);
i.putExtra("mID", mid);
i.putExtra("name", Name.getText().toString());
i.putExtra("qty", Qty.getText().toString());
i.putExtra("price", Price.getText().toString());
i.putExtra("stock", Stock.getText().toString());
i.putExtra("rqQty", RqQty.getText().toString());
startActivity(i);
Toast.makeText(MaterialView.this, "Added Succesfully.", Toast.LENGTH_LONG).show();
}
} );
我已經使用了Intent來傳遞值(我很確定此方法是錯誤的,我還嘗試調用MaterialCart類本身來訪問arrayList,以便我可以添加值,但它不起作用)
這是我的MaterialCart中用於接收值的代碼塊
public class MaterialCart extends Activity {
final ArrayList<PropertyCartTable> materialProperties = new ArrayList<>();
@SuppressLint("LongLogTag")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_material_cart);
Intent i = new Intent();
Bundle extras = getIntent().getExtras();
try{
String Name = extras.getString("name");
String Qty = extras.getString("qty");
String Price = extras.getString("price");
String Stock = extras.getString("stock");
String RqQty = extras.getString("rqQty");
String ID = extras.getString("mID");
Log.d("EXTRAS:", Name + " " + Qty + " " + ID);
materialProperties.add(new PropertyCartTable( ID,Name,Qty,Price,Stock,RqQty));
getIntent().removeExtra("Name");
getIntent().removeExtra("Qty");
getIntent().removeExtra("Price");
getIntent().removeExtra("Stock");
getIntent().removeExtra("RqQty");
getIntent().removeExtra("MID");
}
catch (Exception h){
Log.d("Exception!",h.toString());
}
// materialProperties.add(array);
Log.d("MaterialView.Cart isEmpty", String.valueOf(materialProperties.isEmpty()));
if(materialProperties.isEmpty()) {
Toast.makeText(this, "You have no materials to request.", Toast.LENGTH_LONG).show();
i = new Intent(MaterialCart.this, ProductDetails.class);
startActivity(i);
}else{
ArrayAdapter<PropertyCartTable> adapter = new propertyArrayAdapter(this, 0, materialProperties);
ListView listView = (ListView) findViewById(R.id.lv_materialcart);
listView.setAdapter(adapter);
}
}
這些代碼可以用於接收值,但是當我返回materialView(或選擇其他產品)時,ArrayList不會附加值。
我在這里想要實現的是將MaterialView中的值(即使用戶添加了許多產品)添加到MaterialCart的ArrayList中。
您可以讓您的Application
包含數據:
public class MyApp extends Application {
private static List<String> data = new ArrayList<>();
public static void addItem(String item) {
data.add(item);
}
public static List<String> getData() {
return data;
}
}
並單擊按鈕時:ButtonAdd.setOnClickListener(new View.OnClickListener(){
public void onClick (View view){
MyApp.addItem(your item);
Intent i=new Intent(MaterialView.this, MaterialCart.class);
startActivity(i);
}
} );
在MaterialCart.class中:
List<String> data = MyApp.getData();
但請記住: 關閉應用程序后,數據將清晰可見 。如果要將其保存在本地,則需要使用SharedPreferences
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.