调用notifyitemsetchanged()之后,RecyclerView Adapter数据正在重置

时间:2018-08-18 07:18:48

标签: android android-recyclerview recycler-adapter

我有一个购物车,我想单击按钮来增加/减少物品的数量。我可以看到该应用程序正在进行更改,但是仅当它更改时,它可能会更改回默认值,这可能是由于notifyitemsetchanged()吗?请检查onClick(),我不确定为什么将它“重置”为默认值,而我增加了该值,但它却在几分之一秒内就变回来了:

适配器:

public class CartRecyclerAdapter extends RecyclerView.Adapter<CartRecyclerAdapter.MyViewHolder>  {

Context context;
String json;
ListSharedPreference.Get getSharedPreference;

public CartRecyclerAdapter(Context context, String json) {
    this.context = context;
    this.json = json;
    getSharedPreference = new ListSharedPreference.Get(context);
}

public static <T> List<T> stringToArray(String s, Class<T[]> clazz) {
    return Arrays.asList(new Gson().fromJson(s, clazz));
}

public void setItemList(String itemList) {
    this.itemList = itemList;
    notifyDataSetChanged();
}

@Override
public CartRecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart, parent, false);
    return new CartRecyclerAdapter.MyViewHolder(view);
}

@Override
public void onBindViewHolder(CartRecyclerAdapter.MyViewHolder holder, int position) {
    holder.title.setText(stringToArray(json, RestaurantModel.Menu[].class).get(position).getFoodtitle());
    holder.price.setText(String.format("%d USD", stringToArray(json, RestaurantModel.Menu[].class).get(position).getPrice()));
    holder.quantity.setText(String.valueOf(stringToArray(json, RestaurantModel.Menu[].class).get(position).getQuantity()));
}

@Override
public int getItemCount() {
    if (itemList != null) {
        return stringToArray(json, RestaurantModel.Menu[].class).size();
    }
    return 0;
}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView title;
    TextView price;
    TextView quantity;
    Button add;

    public MyViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.cardView_name);
        price = (TextView) itemView.findViewById(R.id.item_price);
        add = (Button) itemView.findViewById(R.id.add_button);
        quantity = (TextView) itemView.findViewById(R.id.quantity);

        add.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_button:
                int i = Integer.parseInt(quantity.getText().toString());
                i++;
                quantity.setText(String.valueOf(i));
                stringToArray(json, RestaurantModel.Menu[].class).get(getAdapterPosition()).setQuantity(i);
                notifyItemChanged(getAdapterPosition());
                break;
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

问题在于,每次调用stringToArray()时,它都会根据json对象返回一个新数组。因此,显然这个新数组具有所有默认值,但您的Adapter根据它们重新绑定视图。这就是为什么您需要一个数组,以便每次更改其元素并调用notifyItemChanged()/notifyDataSetChanged() onBindViewHolder时都会从中获取值,而不是从其他数组中获取值。它可能如下所示:

public class CartRecyclerAdapter extends RecyclerView.Adapter<CartRecyclerAdapter.MyViewHolder>  {    
    Context context;
    private List<RestaurantModel.Menu> menus = new ArrayList<>();
    ListSharedPreference.Get getSharedPreference;

    public CartRecyclerAdapter(Context context, String json) {
        this.context = context;
        menus = stringToArray(json, RestaurantModel.Menu[].class);
        getSharedPreference = new ListSharedPreference.Get(context);
    }

    public static <T> List<T> stringToArray(String s, Class<T[]> clazz) {
        return Arrays.asList(new Gson().fromJson(s, clazz));
    }

    public void setItemList(String itemList) {
        menus = stringToArray(json, RestaurantModel.Menu[].class);
        notifyDataSetChanged();
    }

    @Override
    public CartRecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart, parent, false);
        return new CartRecyclerAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CartRecyclerAdapter.MyViewHolder holder, int position) {
        holder.title.setText(menus.get(position).getFoodtitle());
        holder.price.setText(String.format("%d USD", menus.get(position).getPrice()));
        holder.quantity.setText(String.valueOf(menus.get(position).getQuantity()));
    }

    @Override
    public int getItemCount() {
        menus.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView title;
        TextView price;
        TextView quantity;
        Button add;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.cardView_name);
            price = (TextView) itemView.findViewById(R.id.item_price);
            add = (Button) itemView.findViewById(R.id.add_button);
            quantity = (TextView) itemView.findViewById(R.id.quantity);

            add.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.add_button:
                    int i = Integer.parseInt(quantity.getText().toString());
                    i++;
                    menus.get(getAdapterPosition()).setQuantity(i);
                    notifyItemChanged(getAdapterPosition());
                    break;
            }
        }
    }
}