在我的安卓应用程序中滚动列表视图时,数据正在消失.我正在使用片段中的列表视图



我正在使用bottomnavigation fragment内的listview。每当屏幕外的listview行的值都会重置。

我试图在我的代码中实现Recyclerview.Viewholder它显示一些错误,我不知道如何在我的代码中实现viewholder

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;
}

}

        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();
                }
}

相关内容

  • 没有找到相关文章

最新更新