2016-08-28 41 views
-1

我想與一個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

​​

回答

0

的問題是,你傳遞數組到第二適配器作爲Parcelable,所以它基本上使得陣列的深層副本,並具有與原始數組無關。因此第二個數組中的更改不會顯示在第一個數組中。
一個簡單的(但也許是骯髒的)解決方案是將數組作爲靜態變量(或應用程序類或單例的一部分),並在兩個活動中使用實際的數組。
如果您可以設計,這兩個列表都在兩個片段中,並且是一個活動的一部分,那麼只需讓這兩個片段的共享數組保持活動狀態即可。

相關問題