Я пытаюсь использовать два различных ArrayAdapter (отдельно для двух деятельности) с одним Arraylist. Первый ArrayAdapter выполняет увеличение для количества, а второе выполняет Decrement. Затем все данные из первого массива ArrayAdapter будут переданы во второй ArrayAdapter, но будут отображаться только данные с количеством больше 1. Кажется, что все предыдущее задание работает нормально, пока я не попытался уменьшить количество (оно показывает, что данные обманываются) и попытайтесь проверить, были ли сохранены изменения, возвращаясь к первому действию и обратно ко второму действию. Случается, что изменения, сделанные Decrement, игнорируются и сбрасывают данные, когда элемент был увеличен. Я знаю, что это немного испортилось, но я надеюсь, что ты сможешь помочь мне, что я делаю неправильно.Как обновить данные в одном ArrayList с двумя arrayadapter в различной активности
EDIT: Я использую два ArrayAdapter, потому что я хочу, чтобы отобразить количество во втором адаптере, наряду с другими элементами.
Вот мои коды:
myProductAdapter.java (Increment адаптер)
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];
}
};
}
Me 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
Bundle bundle = getIntent().getExtras();
productList = bundle.getParcelableArrayList("productList");
filter = new ArrayList<myProduct>();
orderAdapter = new myOrderAdapter(getApplicationContext(),filter);
for(myProduct item : productList){
if(item.productQuantity > 0){
Log.d(TAG,item.getProductName());
Log.d(TAG,"" + item.productQuantity());
filter.add(item);
}
else if(item.productQuantity <= 0){
filter.remove(item);
}
}
orderListView.setAdapter(orderAdapter);