Android Studio 将 RecyclerView 中的文本错误设置为 TextView
Android Studio wrong text is set to TextView in RecyclerView
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.
我已经尝试解决这个问题大约一个星期了。当我用 itemTexte2.get(i).toString() 制作 Toast 消息 时,它显示赖特文本,但是当我我将它添加到 TextView 中,在回收站视图中显示了错误的文本
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();
}
}
您已将 itemTextView
定义为 static TextView
。这意味着您的所有项目只有其中一个。每个视图持有者需要一个。
删除 itemTextView
的静态定义并在视图持有者 ViewHolderKlasse
中定义一个名为 itemTextView
的 class 成员变量。然后,您将在 onBindViewHolder()
中设置此变量,如下所示:
viewHolderKlasse.itemTextView.setText(itemTexte2.get(i).toString());
这应该可以解决这个问题。您还有其他需要处理的静态字段。
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.
我已经尝试解决这个问题大约一个星期了。当我用 itemTexte2.get(i).toString() 制作 Toast 消息 时,它显示赖特文本,但是当我我将它添加到 TextView 中,在回收站视图中显示了错误的文本
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();
}
}
您已将 itemTextView
定义为 static TextView
。这意味着您的所有项目只有其中一个。每个视图持有者需要一个。
删除 itemTextView
的静态定义并在视图持有者 ViewHolderKlasse
中定义一个名为 itemTextView
的 class 成员变量。然后,您将在 onBindViewHolder()
中设置此变量,如下所示:
viewHolderKlasse.itemTextView.setText(itemTexte2.get(i).toString());
这应该可以解决这个问题。您还有其他需要处理的静态字段。