英文:
in RecyclerView, in OnBindViewHolder method
问题
在RecyclerView
中,OnBindViewHolder
方法中,我无法获取位于ViewHolder
类中的TextViews。为什么?问题出在哪里?
请参考截屏:
以下是我的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout)).setTitle("屏幕标题");
RecyclerView rv = findViewById(R.id.recyclerview);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new RecyclerView.Adapter<RecyclerView.ViewHolder>() {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
viewHolder.text1.setText("Bacon");
viewHolder.text2.setText("Bacon ipsum dolor amet pork belly meatball kevin spare ribs. Frankfurter swine corned beef meatloaf, strip steak.");
}
@Override
public int getItemCount() {
return 30;
}
});
}// on create method END
private static class ViewHolder extends RecyclerView.ViewHolder {
TextView text1;
TextView text2;
public ViewHolder(View itemView) {
super(itemView);
text1 = itemView.findViewById(android.R.id.text1);
text2 = itemView.findViewById(android.R.id.text2);
}
}
}
英文:
In RecyclerView
, the OnBindViewHolder
method , I can not get the TextViews that are located in the ViewHolder
class.
Why? What is the problem?
Please refer to the screenshot:
And my code below:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout)).setTitle("Screen Title");
RecyclerView rv = findViewById(R.id.recyclerview);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new RecyclerView.Adapter<RecyclerView.ViewHolder>() {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
viewHolder.text1.setText("Bacon");
viewHolder.text2.setText("Bacon ipsum dolor amet pork belly meatball kevin spare ribs. Frankfurter swine corned beef meatloaf, strip steak.");
}
@Override
public int getItemCount() {
return 30;
}
});
}// on create method END
private static class ViewHolder extends RecyclerView.ViewHolder {
TextView text1;
TextView text2;
public ViewHolder(View itemView) {
super(itemView);
text1 = itemView.findViewById(android.R.id.text1);
text2 = itemView.findViewById(android.R.id.text2);
}
}
}
答案1
得分: 0
我认为可能的原因之一是您将 ViewHolder 放在了 RecyclerViewAdapter 类外部。请将 ViewHolder 放在其中,或尝试将两个 TextView 都保持为 public。
在输入了所有这些后,我意识到这并没有回答您的问题,所以我会将它保留在下面,以防以后您想参考它。
所以,您的方式不会起作用。您需要有一个包含对象的 ArrayList,每个对象都包含要放置的信息。我将添加代码以更好地解释:
在您的示例中,每个单元格只包含一个 TextView,因此创建一个模型类并为其命名。
public class Model {
// 存储文本的变量
private String text;
// 文本的构造函数
public Model(String text) {
this.text = text;
}
// 为文本变量添加设置器和获取器
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
这个模型将包含要显示的信息。现在在您的 RecyclerView 类中创建一个 ArrayList:
// 您也可以在构造函数中初始化它。
private ArrayList<Model> cellsList = new ArrayList<>();
// 设置ArrayList
public void setList(ArrayList<Model> list) {
cellsList = list;
notifyDataSetChanged();
}
最后,在 onBindViewHolder 方法中,使用 ArrayList 中的项目为每个单元格设置属性。
英文:
I think one thing might be that you kept the ViewHolder outside the RecyclerViewAdapter class. Keep the ViewHolder inside it or try keeping both the TextViews public.
After typing all this I realized that it doesn't answer your question so I'll just keep it below in case you would want to refer to it later.
So it won't work the way you have it. You need to have an ArrayList containing objects which will each contain info for what to place. I'll add code to explain better:
In your example each cell only contains a TextView, so create a model class and name it whatever you want.
public class Model{
//Variable that will store the text
private String text;
//Constructor for the text
public Model(String text){
this.text = text;
}
//Add setters and getters for the text variable as well.
public String getText(){return text}
public void setText(String text){
this.text = text;
}
}
This model will contain the info to be displayed. Now create an ArrayList in your RecyclerView class:
//You can initialize in Constructor as well.
private ArrayList<Model> cellsList = new ArrayList<>();
//Set the ArrayList
public void setList(ArrayList<Model> list){
cellsList = list;
notifyDataSetChanged();
}
Finally, within the onBindViewHolder method, set the attributes for each cell using the items in the ArrayList.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论