当用户单击项目的正负btn时,我想实现购物车功能,该项目应保存在某个位置,如果他再次单击同一项目,则数量不必更新列表中的所有项目。这意味着在单击时,该项目的btn json对象将被保存为分片首选项,但是再次单击同一项目时,仅数量将不会更新整个json对象。
public class ItemListNewAdap extends RecyclerView.Adapter<ItemListNewAdap.MyViewHolder> {
private List<PromotionModel> moviesList;
int ii=-1;
Context context;
JSONArray jsonArray = new JSONArray();
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, quantity, price,newprice,num;
public Button btnIncrease,btnDecrease;
ImageView mitemimg;
public MyViewHolder(View view) {
super(view);
mitemimg = (ImageView) view.findViewById(R.id.grid_image);
title = (TextView) view.findViewById(R.id.griddetail_text);
quantity = (TextView) view.findViewById(R.id.quantity_text);
price = (TextView) view.findViewById(R.id.price_text);
newprice = (TextView) view.findViewById(R.id.newprice_text);
num = (TextView) view.findViewById(R.id.num_text);
btnIncrease =view.findViewById(R.id.plus_text);
btnDecrease = view.findViewById(R.id.minus_text);
}
}
public ItemListNewAdap(Context context,List<PromotionModel> moviesList) {
this.context = context;
this.moviesList = moviesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.menusubitemlayout, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final PromotionModel movie = moviesList.get(position);
holder.title.setText(movie.getHeading());
holder.price.setText(movie.getOldprice());
holder.newprice.setText(movie.getNewprice());
holder.quantity.setText(movie.getQuantity());
Picasso.with(context).load(movie.getImage()).placeholder(R.drawable.download).error(R.drawable.download).into(holder.mitemimg);
holder.btnIncrease.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
ii++;
int count= Integer.parseInt(String.valueOf(holder.num.getText()));
count++;
holder.num.setText(String.valueOf(count));
// ii++;
// holder.num.setText(ii+"");
notifyDataSetChanged();
String chk = moviesList.get(position).getChk();
try {
JSONObject student1 = new JSONObject();
student1.put("pid", moviesList.get(position).getItemid());
student1.put("desc", moviesList.get(position).getDetailtxt());
student1.put("imgurl", moviesList.get(position).getImage());
student1.put("name", moviesList.get(position).getHeading());
student1.put("newprice", moviesList.get(position).getNewprice());
student1.put("oldprice", moviesList.get(position).getOldprice());
student1.put("quantity", moviesList.get(position).getQuantity());
student1.put("unit", count);
jsonArray.put(student1);
Log.d("nlononnono", "" + jsonArray.toString());
// SharedPreferences sharedPrefotp = context.getSharedPreferences("MyPrefcart", Context.MODE_PRIVATE);
// SharedPreferences.Editor editor123 = sharedPrefotp.edit();
// editor123.putString("quantity", jsonArray.toString());
// editor123.commit();
// ((HomeActivity)activity).mcounttxt.setText(String.valueOf(ii));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
holder.btnDecrease.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int count= Integer.parseInt(String.valueOf(holder.num.getText()));
if(count > 0) {
// 2. enter code here
count--;
holder.num.setText(String.valueOf(count));
}
// ii--;
// holder.num.setText(ii+"");
notifyDataSetChanged();
try {
JSONObject student1 = new JSONObject();
student1.put("pid", moviesList.get(position).getItemid());
student1.put("desc", moviesList.get(position).getDetailtxt());
student1.put("imgurl", moviesList.get(position).getImage());
student1.put("name", moviesList.get(position).getHeading());
student1.put("newprice", moviesList.get(position).getNewprice());
student1.put("oldprice", moviesList.get(position).getOldprice());
student1.put("quantity", moviesList.get(position).getQuantity());
student1.put("unit", count);
jsonArray.put(student1);
Log.d("nlononnono", "" + jsonArray.toString());
// Log.d("nlononnono", "" + jsonArray.toString());
// SharedPreferences sharedPrefotp = context.getSharedPreferences("MyPrefcart", Context.MODE_PRIVATE);
// SharedPreferences.Editor editor123 = sharedPrefotp.edit();
// editor123.putString("quantity", jsonArray.toString());
// editor123.commit();
// ((HomeActivity)activity).mcounttxt.setText(String.valueOf(ii));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public int getItemCount() {
return moviesList.size();
}
}
答案 0 :(得分:0)
去年,我对我的一个项目有类似的要求。我通过使用Sharedpreferences存储数据解决了它,其他任何活动基本上都可以访问它。这是我的代码,其中包含两个我用来从购物车中添加和删除产品的主要功能。在您的情况下,您只需修改添加功能即可指定是按加号还是减号按钮来添加或删除列表中的产品
public class Product{
public int id;
public double price;
public String name;
public int quantity;
}
public class Storage{
Context mContext;
public Storage(Context context){
mContext = context;
}
//get your saved cart
public ArrayList<Product> getCartList(){
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(mContext);
Gson gson = new Gson();
String json = sharedPref.getString(ReferenceDataEnums.Cart.name(), null);
Type type = new TypeToken<ArrayList<Product>>(){}.getType();
return gson.fromJson(json, type);
}
public void addToCart(Product product){
ArrayList<Product> products = getCartList();
if (products == null){
//Cart is empty. Therefore product does not exist in the cart
products = new ArrayList<>();
products.add(product);
}
else{
boolean productExists = false;
while (iterator.hasNext())
{
if(iterator.next().id.equals(product.id))
{
//Increment current quantity
productExists = true;
break;
}
}
if(productExists)
{
iterator.next().quantity++;
}
else{
//Product does not exist in the cart
products = new ArrayList<>();
products.add(product);
}
}
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = sharedPref.edit();
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<Product>>(){}.getType();
String json = gson.toJson(products, type);
editor.putString(ReferenceDataEnums.Cart.name(), json);
editor.apply();
}
}
希望有帮助