我试图像购物车一样创建应用 使用它来访问我的数据库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中。
答案 0 :(得分:2)
您可以让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