androidlistviewscrollcustom-arrayadapter

While Scrolling the listview in my android app the data is disappearing.I am using the listview inside a Fragment


I am working with a listview inside a fragment of bottomnavigation. Whenever the listview row going outside the Screen it values resets.

I have tried to implement Recyclerview.Viewholder in my code its showing some errors and i dont know how to implement viewholder in my code.

public class AtcAdapter  extends ArrayAdapter<AtcObjects> {
    public AtcAdapter(@NonNull Context context, int resource, List<AtcObjects> objects) {
        super(context, resource, objects);
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
    View ListItemView=convertView;

    if(ListItemView==null) {
        ListItemView = LayoutInflater.from(getContext()).inflate(R.layout.atc_list, parent, false);

        }
    AtcObjects currentFood = getItem(position);

    TextView foodName=(TextView) ListItemView.findViewById(R.id.atcFname);
    foodName.setText(currentFood.getpName());
    TextView fPrice=(TextView) ListItemView.findViewById(R.id.atcFprice);
    fPrice.setText("Price   :"+currentFood.getpPrice()+" RS");
    TextView tvRes=(TextView) ListItemView.findViewById(R.id.tvResName);
    tvRes.setText(""+currentFood.getResName()+" ResName");
    String url=currentFood.getpIng();
    ImageView imgV=(ImageView) ListItemView.findViewById(R.id.ivAtc);
    Picasso.get().load(url).into(imgV);
    final TextView textView=(TextView) ListItemView.findViewById(R.id.atcQty);
    textView.setText(currentFood.qty+"");
    Button addBtn=(Button) ListItemView.findViewById(R.id.atc_addBtn);
  Button btnRemove=(Button) ListItemView.findViewById(R.id.atc_delete);
    btnRemove.setOnClickListener(new View.OnClickListener() {
                                     @Override
                                     public void onClick(View view) {
                                         ((ListView) parent).performItemClick(view, position, 66);
                                     }
                                 });
           addBtn.setTag(position);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           updateQuantity(position,textView,1);
            ((ListView) parent).performItemClick(view, position, 56);

        }
    });
    Button subbtn=(Button) ListItemView.findViewById(R.id.atc_subBtn);
    subbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
         updateQuantity(position,textView,-1);
            ((ListView) parent).performItemClick(view, position, 90);

        }
    });
    return ListItemView;
}


private void updateQuantity(int position, TextView edTextQuantity, int value) {

   AtcObjects products = getItem(position);
    if(value > 0)
    {
        products.qty = products.qty + 1;
    }
    else
    {
        if(products.qty > 0)
        {
            products.qty = products.qty - 1;
        }

    }
    edTextQuantity.setText(products.qty+"");
}
@Override
public int getViewTypeCount() {

    return getCount();
}

@Override
public int getItemViewType(int position) {

    return position;
}

}


Solution

  •         You can create ViewHolder class as below:
             static class ViewHolder {
                    ImageView imageView;
                    TextView textView;
                    int position;
                }
    
            and inside getView() method use below code :-
            @Override
            public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
             ViewHolder holder;
    
                    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.view, null);
                        holder = new ViewHolder();
                        holder.textView= (TextView) convertView.findViewById(R.id.textView);
                        convertView.setTag(holder);
                    }
                    else {
                        holder = (ViewHolder) convertView.getTag();
                    }
    }