我正在使用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();
}
}