【发布时间】:2017-03-16 06:35:59
【问题描述】:
我正在尝试创建一个类似于购物车的应用 使用它来访问我的数据库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。
【问题讨论】:
-
你需要结合使用 SharedPrefs 和 startActivityForResult 方法
-
你有2个选项,你可以使用
StartActivityForResult或者将其转换为使用fragment并将其实例保存在backstack中。 -
@SecretCoder StartActivityForResult,它也可以存储多个arraylistvalues吗?我找到了这个stackoverflow.com/a/40969871/3681880,它显示了从第二个活动到主要活动的一串值。可以将其视为我可以将值存储到的临时表列表吗?我将总共访问三个活动。我三天前才开始使用android studio,这让我很困惑。
-
@kol 是的,它可以使用
Bundle。将您的数组打包成一个包并将其传递给您的startActyivityForResult -
@SecretCoder 我一直收到一个错误,一些与 parcelable 相关的错误,我试图传递整个 ArrayList
所以我尝试使用 ArrayList 来做另一种方法,但结果以当我将 ArrayList 传递给 ArrayList 时出现同样的错误。我也试过小远的回答,但还是报同样的错误。