I have some ArrayLists where Products are inserted and I have the problem, that in itemTextView is set an text, which is not in this ArrayList. What am I doing wrong? Please answer.
I have tried to solve this problem for like about one week. When I'm making a Toast message with itemTexte2.get(i).toString() it shows the wright text, but when I'm adding it to the TextView with shows the wrong text in the recycler view
public class RvAdapterKlasse extends RecyclerView.Adapter<RvAdapterKlasse.ViewHolderKlasse> {
DatabaseHelper mDatabaseHelper;
static TextView itemTextView, unitItemTextView;
static ImageView itemImageView;
static CheckBox checkBox;
static ArrayList<Integer> positions = new ArrayList();
public class ViewHolderKlasse extends RecyclerView.ViewHolder {
public ViewHolderKlasse(@NonNull final View itemView) {
super(itemView);
mDatabaseHelper = new DatabaseHelper(itemView.getContext());
itemTextView = itemView.findViewById(R.id.textViewItem);
itemImageView = itemView.findViewById(R.id.imageViewItem);
unitItemTextView = itemView.findViewById(R.id.unitTextViewItem);
checkBox = itemView.findViewById(R.id.checkBox);
}
}
@NonNull
@Override
public ViewHolderKlasse onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout1, null);
return new ViewHolderKlasse(itemView1);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolderKlasse viewHolderKlasse, final int i) {
Toast.makeText(unitItemTextView.getContext(), itemTexte2.get(i).toString(), Toast.LENGTH_SHORT).show();
itemTextView.setText(itemTexte2.get(i).toString());
unitItemTextView.setText(itemTexte2.get(i).toString());
itemImageView.setImageResource(R.drawable.dots);
viewHolderKlasse.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
viewHolderKlasse.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent intent = new Intent(v.getContext(), PriceActivity.class);
String text = ((TextView) MainActivity.recyclerView1.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.textViewItem)).getText().toString().trim();
intent.putExtra("product", text);
v.getContext().startActivity(intent);
return false;
}
});
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
}
@Override
public int getItemCount() {
return itemTexte2.size();
}
}
You have defined itemTextView
as a static TextView
. This means that you only have one of these for all of your items. You need one for each view holder.
Remove the static definition for itemTextView
and define a class member variable named itemTextView
in your view holder ViewHolderKlasse
. You will then set this variable in onBindViewHolder()
like this:
viewHolderKlasse.itemTextView.setText(itemTexte2.get(i).toString());
This should solve this problem. You have other static fields that you will also need to address.