I have this list view where you can select the items to buy. When you click on an item a group of elements appears. (The checkbox, the 2 buttons, and the textview.) Where you can select the quantity of the product that you want to buy.
This group of elements is hidden in the Layout. The visibility of this group of elements changes from VIEW.GONE to VIEW.VISIBLE
The problem is that the buttons appear randomly in other rows. For example, when I click on the THIN BEER the group of elements appears on the view, but when I scroll down the app, the group of elements also appear on the HAMBURGER row and other.
And when I change the value on one of the textview, also increase in the other elements.
public class ProductAdapter extends ArrayAdapter<Product> {
ArrayList<Boolean> positionArray;
public ProductAdapter(Context context, ArrayList<Product> products){
super(context,0,products);
positionArray = new ArrayList<Boolean>(products.size());
for(int i =0;i<products.size();i++){
positionArray.add(false);
}
}
public static class ViewHolder{
TextView tv_price,tv_prodName, tv_details, tv_location;
ImageView imgView_productImage;
CheckBox checkBox;
Button bntUp,btnDown;
}
//When a listview recycles views , it recycles its present state as well as listeners attached to it.if the checkbox was checked and has a
// onCheckedChangeListener set, both will remain a part of recycled view based on position.
// So it is our responsibility to reset all states and remove previous listeners.
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
Product product = getItem(position);
if(convertView==null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_product_item,parent, false);
viewHolder = new ViewHolder();
viewHolder.imgView_productImage = (ImageView)convertView.findViewById(R.id.list_item_imgView_productImage);
viewHolder.tv_price = (TextView)convertView.findViewById(R.id.listItem_tv_price);
viewHolder.tv_prodName = (TextView)convertView.findViewById(R.id.listItem_tv_productName);
viewHolder.tv_location = (TextView)convertView.findViewById(R.id.listItem_tv_location);
viewHolder.tv_details = (TextView)convertView.findViewById(R.id.listItem_tv_productDetail);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.listProd_checkbox);
viewHolder.bntUp = (Button)convertView.findViewById(R.id.listProd_up);
viewHolder.btnDown = (Button)convertView.findViewById(R.id.listProd_down);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
viewHolder.checkBox.setOnCheckedChangeListener(null); //
}
viewHolder.tv_price.setText("$" + String.valueOf(product.getPrice()));
viewHolder.tv_prodName.setText(product.getProductName());
viewHolder.tv_location.setText(product.getLocation());
viewHolder.tv_details.setText(product.getProductDetail());
viewHolder.imgView_productImage.setImageBitmap(MifareUtilities.getDecodeImage(product.getProductImage()));
viewHolder.checkBox.setChecked(positionArray.get(position));
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
positionArray.set(position,true);
}else {
positionArray.set(position,false);
}
}
});
return convertView;
}
`}
And this is the code that is on my Fragment, where I show up the listView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
productsList = new HashMap<>();
final View viewroot = inflater.inflate(R.layout.fragment_payment, container, false);
final ArrayList<Product> products = (ArrayList<Product>)new ProductController(new ProductFromString()).getProducts();
final ListView listView = (ListView)viewroot.findViewById(R.id.fragPayment_listview);
ProductAdapter productAdapter = new ProductAdapter(getActivity(),products);
listView.setAdapter(productAdapter);
listView.setLongClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
final int positionf = position;
final CheckBox checkBox = (CheckBox)view.findViewById(R.id.listProd_checkbox);
checkBox.setVisibility(View.VISIBLE);
checkBox.setChecked(!checkBox.isChecked());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if(!checkBox.isChecked()){
productsList.remove(String.valueOf(position));
}else {
Product product = (Product) listView.getAdapter().getItem(positionf);
productsList.put(String.valueOf(position),product);
}
}
});
final Button btnUp = (Button)view.findViewById(R.id.listProd_up);
final Button btnDown = (Button)view.findViewById(R.id.listProd_down);
final TextView quantity = (TextView)view.findViewById(R.id.listItem_quantity);
btnUp.setTag(position);
btnDown.setTag(position);
btnDown.setVisibility(View.VISIBLE);
btnUp.setVisibility(View.VISIBLE);
quantity.setVisibility(View.VISIBLE);
btnDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//String q = quantity.getText().toString();
int quan = Integer.parseInt(quantity.getText().toString());
//int btnPosition = (Integer)v.getTag();
if(quan!=1){
quan --;
quantity.setText(String.valueOf(quan));
updateProductQuantity(listView,(Integer)v.getTag(),Integer.parseInt(quantity.getText().toString()) );
}else { //if the quantity is 0 it will hide the buttons and uncheck
btnDown.setVisibility(View.GONE);
btnUp.setVisibility(View.GONE);
checkBox.setChecked(false);
quantity.setVisibility(View.GONE);
checkBox.setVisibility(View.GONE);
//remove product from list
productsList.remove(String.valueOf((Integer)v.getTag()));
}
}
});
btnUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int quan = Integer.parseInt(quantity.getText().toString());
quan++;
quantity.setText(String.valueOf(quan));
// updateProductQuantity(listView,(Integer)v.getTag(),Integer.parseInt(quantity.getText().toString()) );
}
});
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final Product product = (Product) listView.getAdapter().getItem(position);
//dlgShowConfig(product);
Intent intentActivityPayment = new Intent(getActivity(), ActivityPayment.class);
intentActivityPayment.putExtra("productObject",product);
startActivity(intentActivityPayment);
return true; //evitara que se ejecute el onitemclicklistener cuando ejecutes el onitemlongclicklisstener
}
});
return viewroot;
}