Vijay Vijay - 1 month ago 6
Android Question

While Scrolling RecyclerView, custom-view value keep changing

I have issue that when I click to increase quantity of position first item and when I scolling down and up then, position of that item is flicurring...

Please help me out guys Thanks in Advanced

This is my adapter class.

package growcia.malacus.com.growcia.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import growcia.malacus.com.growcia.R;
import growcia.malacus.com.growcia.activity.ProductListActivity;
import growcia.malacus.com.growcia.database.SqliteDatabaseClass;
import growcia.malacus.com.growcia.model.SellerProductPOJO;



public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {

public Context context;
SqliteDatabaseClass DB;
String productCode;
boolean isPressed = false;
int count = 0;
int qty;


public String pname, price, img_path;
static int productItem = 0;
int totPrice;

ProductListActivity objProductList;
List<SellerProductPOJO> productListDetails;


public ProductListAdapter(List<SellerProductPOJO> productDetails, Context context) {
super();

DB = new SqliteDatabaseClass(context);
objProductList = new ProductListActivity();
this.productListDetails = productDetails;
this.context = context;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_product, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

final SellerProductPOJO objSellerProductPOJO = productListDetails.get(position);

try {
JSONArray jar = DB.getAllProductCodeAndQtyProductList();
Log.e("total pid and qty ad : ", "" + jar.toString());

for (int i = 0; i < jar.length(); i++) {
JSONObject job = jar.getJSONObject(i);

String cart_productId = job.getString("ProductCode");
String productQty = job.getString("QuantityOrdered");

Log.e("id and qty: ", cart_productId + " qty: " + productQty);

String plist_prod_id = productListDetails.get(position).getProductCode();

Log.e("product id in cart : ", "" + cart_productId.toString());
Log.e("product id service : ", "" + plist_prod_id.toString());

}

} catch (JSONException J) {
J.printStackTrace();
}


String url = objSellerProductPOJO.getImagePath();
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder) // optional
.error(R.drawable.error) // optional
.resize(100, 100) // optional
.into(holder.ivProduct);
holder.tvProductName.setText(objSellerProductPOJO.getProductName());
holder.tvUnit.setText(objSellerProductPOJO.getAvailableQuantity());
holder.tvPrice.setText(objSellerProductPOJO.getPrice());

Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/abc.ttf");
holder.tvProductName.setTypeface(font);
holder.tvUnit.setTypeface(font);
holder.tvPrice.setTypeface(font);


if (context instanceof ProductListActivity)
totPrice = DB.getSumPrice();
Log.e("all price insert : ", "" + totPrice);
count = DB.getProfilesCount();
Log.e("count from db", "" + count);
((ProductListActivity) context).showCartItem(count, totPrice);



holder.btnPlus.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
SellerProductPOJO objSellerProduct = productListDetails.get(position);


String stock = holder.tvUnit.getText().toString();
int qtyMiddle = Integer.parseInt(holder.tvQty.getText().toString());
int qtyStock = Integer.parseInt(objSellerProduct.getAvailableQuantity().toString());


if (!stock.equalsIgnoreCase("0")) {

if (qtyMiddle < qtyStock) {


pname = objSellerProduct.getProductName();
img_path = objSellerProduct.getImagePath();
price = objSellerProduct.getPrice();
productCode = objSellerProduct.getProductCode();


String str_qty = holder.tvQty.getText().toString();
int qty = Integer.parseInt(str_qty);

qty = qty + 1;
String final_str_qty = "" + qty;


objSellerProductPOJO.setQty(final_str_qty);
holder.tvQty.setText(objSellerProductPOJO.getQty() + "");

int reduceable_stock = qtyStock - qty;

holder.tvUnit.setText(reduceable_stock + "");

if (qty > 0) {
boolean entryStatus = DB.Exists(productCode);
if (entryStatus) {

productItem = productItem + 1;
String str_newQty = holder.tvQty.getText().toString();
int newqty = Integer.parseInt(str_newQty);
double intPrice = Double.parseDouble(price);
double totPrice = qty * intPrice;
DB.updateProductQty(productCode, newqty, totPrice);
totPrice = DB.getSumPrice();
Log.e("all price update: ", "" + totPrice);

} else {
productItem = 1;
DB.addProductItem(productCode, pname, img_path, productItem, price, price);

}
if (context instanceof ProductListActivity)
totPrice = DB.getSumPrice();
Log.e("all price insert : ", "" + totPrice);
count = DB.getProfilesCount();
Log.e("count from db", "" + count);
((ProductListActivity) context).showCartItem(count, totPrice);
}


} else {
Toast.makeText(context, "Product out of stock!!", Toast.LENGTH_SHORT).show();
}

}

}
});

holder.btnMinus.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

String stock = holder.tvUnit.getText().toString();
if (!stock.equalsIgnoreCase("0")) {

SellerProductPOJO objSellerProductDeduct = productListDetails.get(position);

String str_qty = holder.tvQty.getText().toString();
int qty = Integer.parseInt(str_qty);

if (qty != 0) {

int qtyStockMinusClick = Integer.parseInt(holder.tvUnit.getText().toString());

holder.tvUnit.setText((qtyStockMinusClick + 1) + "");



Log.e("btnMinus", "" + qty);

if (qty == 1) {
Log.e("", "inside 0 qty");
DB.delete_byID(productCode);
qty = qty - 1;
String final_str_qty = "" + qty;

objSellerProductPOJO.setQty(final_str_qty);
holder.tvQty.setText(objSellerProductPOJO.getQty()+"");


} else {

qty = qty - 1;
String final_str_qty = "" + qty;

objSellerProductPOJO.setQty(final_str_qty);
holder.tvQty.setText(objSellerProductPOJO.getQty()+"");


double intPrice = Double.parseDouble(price);
double totPrice = qty * intPrice;

DB.updateProductQty(productCode, qty, totPrice);
}

if (context instanceof ProductListActivity)
totPrice = DB.getSumPrice();
Log.e("all price insert : ", "" + totPrice);
count = DB.getProfilesCount();
Log.e("count from db", "" + count);
((ProductListActivity) context).showCartItem(count, totPrice);

}
} else {
Toast.makeText(context, "Product out of stock!!", Toast.LENGTH_SHORT).show();
}
notifyDataSetChanged();
}
});


holder.imagefavorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Log.e("position", "all position" + position);
if (isPressed)
holder.imagefavorite.setBackgroundResource(R.drawable.ic_not_favourite);
else
holder.imagefavorite.setBackgroundResource(R.drawable.favourite_icon);

isPressed = !isPressed;
}
});

}

@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}


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



class ViewHolder extends RecyclerView.ViewHolder {

public TextView tvProductName;
public TextView tvUnit;
public TextView tvPrice;
public TextView tvQty;
public ImageView ivProduct;
public ImageView imagefavorite;
// public EditText edqntiry;
public Button btnPlus;
public Button btnMinus;

public ViewHolder(View itemView) {
super(itemView);
ivProduct = (ImageView) itemView.findViewById(R.id.ivProduct);
tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
tvUnit = (TextView) itemView.findViewById(R.id.tvUnit);
tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
tvQty = (TextView) itemView.findViewById(R.id.tvQty);
btnPlus = (Button) itemView.findViewById(R.id.btnPlus);
btnMinus = (Button) itemView.findViewById(R.id.btnMinus);
imagefavorite = (ImageView) itemView.findViewById(R.id.imagefavorite);
}

}
}


When I am going to incerase qty then below screen shows

enter image description here

when I am scrolled down and up then below screen shows

enter image description here

Answer

You are not setting the initial value of holder.tvQty in your onBindViewHolder method.

When you update the value of holder.tvQty in holder.btnPlus or holder.btnMinus listeners, you should save that value somewhere in your objSellerProductPOJO:

objSellerProductPOJO.setQty(final_str_qty)

Then under:

holder.tvPrice.setText(objSellerProductPOJO.getPrice());

add:

holder.tvQty.setText(objSellerProductPOJO.getQty());