我在Json中放置对象,但我只想要更新一个键。现在每次调用函数时它都会创建新对象我想只有数量应该更新,如果它已经存在于json中。请帮忙
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
private List<ProductBean> productList ;
static int qtyValues;
static int itemRem = -1;
static int addItem = 1;
static boolean isSKU1;
static boolean isSKU2;
static boolean isSKU;
static int value;
static int totalAmount = 0;
static int totalItems = 0;
SharedPreferences myPrefs;
SharedPreferences.Editor editor;
private final Activity context;
private AQuery aq;
JSONObject jsonObject = new JSONObject();
JSONArray obj = new JSONArray();
ArrayList list = new ArrayList();
public ProductAdapter(Activity context,List<ProductBean> productList){
this.productList = productList;
this.context = context;
isSKU = true;
}
@Override
public int getItemCount() {
return productList.size();
}
@Override
public void onBindViewHolder(final ProductViewHolder holder, final int position) {
myPrefs = context.getSharedPreferences("qty", Context.MODE_PRIVATE);
editor = myPrefs.edit();
aq = new AQuery(context.getApplicationContext());
final ProductBean productBean = productList.get(position);
holder.extratxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.extratxt.setTextColor(Color.BLACK);
holder.extratxt2.setTextColor(Color.GRAY);
holder.extratxt3.setTextColor(Color.GRAY);
holder.extratxt1.setText(productBean.getProductPrice() + "");
isSKU = true;
isSKU1 = false;
isSKU2 = false;
holder.qtyCounter.setText(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId1(),0)+"");
}
});
holder.extratxt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.extratxt.setTextColor(Color.GRAY);
holder.extratxt2.setTextColor(Color.BLACK);
holder.extratxt3.setTextColor(Color.GRAY);
holder.extratxt1.setText(productBean.getProductPrice2() + "");
isSKU1 = true;
isSKU = false;
isSKU2 = false;
holder.qtyCounter.setText(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId2(),0)+"");
}
});
holder.extratxt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.extratxt.setTextColor(Color.GRAY);
holder.extratxt2.setTextColor(Color.GRAY);
holder.extratxt3.setTextColor(Color.BLACK);
holder.extratxt1.setText(productBean.getProductPrice3() + "");
isSKU2 = true;
isSKU = false;
isSKU1 = false;
holder.qtyCounter.setText(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId3(),0)+"");
}
});
holder.decreaseQty.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
if(isSKU1 == true) {
if(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId2(),0)>0) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0);
qtyValues = qtyValues - 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId2(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0) + "");
value = 0 - productBean.getProductPrice2();
add(itemRem);
amount(value);
}
}
else if (isSKU2 == true){
if(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId3(),0)>0) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0);
qtyValues = qtyValues - 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId3(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0) + "");
value = 0 - productBean.getProductPrice3();
add(itemRem);
amount(value);
}
}
else{
if(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId1(),0)>0) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0);
qtyValues = qtyValues - 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId1(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0) + "");
value = 0 - productBean.getProductPrice();
add(itemRem);
amount(value);
}
}
}
});
holder.increaseQty.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
if(isSKU1 == true) {
if(myPrefs.getInt("product"+productBean.getProductId() + productBean.getSkuId2(),0)<10) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0);
qtyValues = qtyValues + 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId2(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0) + "");
value = productBean.getProductPrice2();
add(addItem);
amount(value);
setCartItem(productBean.getSkuId2(),myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0),productBean.getProductId(),productBean.getDescription() , productBean.getProductName() , productBean.getProductPrice2());
}
}
else if (isSKU2 == true){
if(myPrefs.getInt("product"+productBean.getProductId() + productBean.getSkuId3(),0)<10) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0);
qtyValues = qtyValues + 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId3(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0) + "");
value = productBean.getProductPrice3();
add(addItem);
amount(value);
setCartItem(productBean.getSkuId3(),myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0),productBean.getProductId(),productBean.getDescription() , productBean.getProductName() , productBean.getProductPrice3());
}
}
else{
if(myPrefs.getInt("product"+productBean.getProductId() + productBean.getSkuId1(),0)<10) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0);
qtyValues = qtyValues + 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId1(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0) + "");
value = productBean.getProductPrice();
add(addItem);
amount(value);
setCartItem(productBean.getSkuId1(),myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0),productBean.getProductId(),productBean.getDescription() , productBean.getProductName() , productBean.getProductPrice());
}
}
}
});
holder.txtTitle.setText(productBean.getProductName());
holder.extratxt.setText(productBean.getSkuId1() + "");
holder.extratxt2.setText(productBean.getSkuId2() + "");
holder.extratxt3.setText(productBean.getSkuId3() + "");
holder.extratxt1.setText(productBean.getProductPrice() + "");
holder.imageView1.setImageResource(R.drawable.rupee);
if(myPrefs.getInt("totalItems", 0)>0){
BeveragesMainActivity.cartTotal.setVisibility(View.VISIBLE);
BeveragesMainActivity.totalPrice.setText("Rs " + myPrefs.getInt("totalAmount", 0) + "");
}
BeveragesMainActivity.cartTotal.setText(myPrefs.getInt("totalItems", 0) + "");
holder.qtyCounter.setText(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId1(),0) + "");
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.list_beverages_item, viewGroup, false);
return new ProductViewHolder(itemView);
}
public class ProductViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView extratxt;
TextView extratxt2;
TextView extratxt3;
ImageView imageView1;
TextView extratxt1;
TextView qtyCounter;
ImageView decreaseQty;
ImageView increaseQty;
/**
* Instantiates a new card view holder.
*
* @param v the v
*/
public ProductViewHolder(View v) {
super(v);
imageView = (ImageView) v.findViewById(R.id.drinks);
txtTitle = (TextView) v.findViewById(R.id.drinkTitle);
extratxt = (TextView) v.findViewById(R.id.drinkQuantity);
extratxt2 = (TextView) v.findViewById(R.id.drinkQuantity1);
extratxt3 = (TextView) v.findViewById(R.id.drinkQuantity2);
imageView1 = (ImageView) v.findViewById(R.id.rupee);
extratxt1 = (TextView) v.findViewById(R.id.drinkPrice);
qtyCounter = (TextView) v.findViewById(R.id.drinkCounter);
decreaseQty = (ImageView) v.findViewById(R.id.decreaseCounter);
increaseQty = (ImageView) v.findViewById(R.id.increaseCounter);
extratxt.setTextColor(Color.BLACK);
}
}
public void setCartItem(final int skuId ,final int qty , final long productId , String description , String productName , int price) {
try {
jsonObject.put("skuId",skuId);
jsonObject.put("quantity",qty);
jsonObject.put("productId",productId);
jsonObject.put("price",price);
jsonObject.put("productName",productName);
jsonObject.put("description",description);
obj.put(jsonObject);
editor.putString("data",obj.toString());
editor.commit();
}catch (JSONException e){
e.printStackTrace();
}
}
public void add(int items){
totalItems = totalItems + items;
editor.putInt("totalItems",totalItems);
editor.commit();
if(myPrefs.getInt("totalItems", 0)>0){
BeveragesMainActivity.cartTotal.setVisibility(View.VISIBLE);
}
else{
BeveragesMainActivity.cartTotal.setVisibility(View.INVISIBLE);
}
BeveragesMainActivity.cartTotal.setText(myPrefs.getInt("totalItems", 0) + "");
}
public void amount(int price){
totalAmount = totalAmount+price;
editor.putInt("totalAmount", totalAmount);
editor.commit();
BeveragesMainActivity.totalPrice.setText("Rs " + myPrefs.getInt("totalAmount", 0) + "");
}
}
答案 0 :(得分:0)
在setCartItem(...)
方法
// loop through each object
for (int i = 0; i < obj.length(); i++) {
try {
JSONObject item = obj.getJSONObject(i);
int itemId = item.getInt("skuId ");
if (itemId == skuId) {// object already exists update quantity
item.put("quantity", qty);
// update other keys if you want to
// do other stuff
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONObject jsonObject = new JSONObject();
// If skudId does not exits in the array list add new one
try {
jsonObject.put("skuId", skuId);
jsonObject.put("quantity", qty);
jsonObject.put("productId", productId);
jsonObject.put("price", price);
jsonObject.put("productName", productName);
jsonObject.put("description", description);
obj.put(jsonObject);
editor.putString("data", obj.toString());
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}