我想與一個Arraylist使用兩個不同的ArrayAdapter(分別在兩個活動)。第一個ArrayAdapter執行Quantity的Increment,第二個執行Decrement。然後,所有來自第一個ArrayAdapter的數據將被傳輸到第二個ArrayAdapter,但只會顯示數量大於1的數據。以前的所有任務似乎都能正常工作,直到我嘗試減少數量(它顯示數據不存在),並嘗試通過返回到第一個活動並返回到第二個活動來檢查是否保存了更改。會發生什麼情況是,遞減所做的更改將被忽略,並會重置項目被增量時的數據。我知道這有點混亂,但我希望你能幫助我,我做錯了什麼。如何更新數據在一個arraylist與兩個arrayadapter在不同的活動
編輯:我使用兩個ArrayAdapter,因爲我想顯示數量在第二個適配器,連同其他元素。
這是我的代碼:
myProductAdapter.java(增量適配器)
public class myProductAdapter extends ArrayAdapter<myProduct> {
public class ViewHolder{
Button addItem;
}
public myProductAdapter(Context context, ArrayList<myProduct> myProducts) {
super(context, 0, myProducts);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final myProduct product = getItem(position);
final ViewHolder viewHolder;
//Some Codes Here..
viewHolder.addItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
product.productQuantity ++;
Toast.makeText(getContext(), "" + product.productQuantity(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
myOrderAdapter.java(Decerement適配器)
public class myOrderAdapter extends ArrayAdapter<myProduct> {
public class ViewHolder{
Button minusItem;
}
public myOrderAdapter(Context context, ArrayList<myProduct> orders){
super(context, 0, orders);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final myProduct order = getItem(position);
viewHolder.minusItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
order.productQuantity --;
if(order.productQuantity() <= 0){
order.productQuantity = 0;
notifyDataSetChanged();
}
Toast.makeText(getContext(),"" + order.productQuantity(),Toast.LENGTH_SHORT).show();
return convertView;
}
}
myProduct.java
public class myProduct implements Parcelable {
@SerializedName("productID")
public int productID;
@SerializedName("categoryID")
public int categoryID;
@SerializedName("productName")
public String productName;
@SerializedName("productPrice")
public int productPrice;
public int productQuantity = 0;
public myProduct(int productID, int categoryID, String productName, int productPrice){
this.productID = productID;
this.categoryID = categoryID;
this.productName = productName;
this.productPrice = productPrice;
}
public int getProductID(){
return productID;
}
public int getCategoryID(){
return categoryID;
}
public String getProductName(){
return productName;
}
public int getProductPrice(){
return productPrice;
}
public int productQuantity() {
return productQuantity;
}
protected myProduct(Parcel in) {
productID = in.readInt();
categoryID = in.readInt();
productName = in.readString();
productPrice = in.readInt();
productQuantity = in.readInt();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(productID);
dest.writeInt(categoryID);
dest.writeString(productName);
dest.writeInt(productPrice);
dest.writeInt(productQuantity);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<myProduct> CREATOR = new Parcelable.Creator<myProduct>() {
@Override
public myProduct createFromParcel(Parcel in) {
return new myProduct(in);
}
@Override
public myProduct[] newArray(int size) {
return new myProduct[size];
}
};
}
我nu.class
viewOrder = (Button)findViewById(R.id.viewOrder);
viewOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Order.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("productList", productList);
i.putExtras(bundle);
startActivity(i);
}
});
Order.class