Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在RecyclerView中,在OnBindViewHolder方法中_Java_Textview - Fatal编程技术网

Java 在RecyclerView中,在OnBindViewHolder方法中

Java 在RecyclerView中,在OnBindViewHolder方法中,java,textview,Java,Textview,在RecyclerView中的OnBindViewHolder方法中,我无法获取位于ViewHolder类中的文本视图。 为什么?有什么问题 请参阅屏幕截图: 我的代码如下: public类MainActivity扩展了AppCompatActivity{ @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

RecyclerView
中的
OnBindViewHolder
方法中,我无法获取位于
ViewHolder
类中的文本视图。 为什么?有什么问题

请参阅屏幕截图:

我的代码如下:

public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((折叠工具栏布局)findViewById(R.id.collasing_toolbar_布局)).setTitle(“屏幕标题”);
RecyclerView rv=findViewById(R.id.RecyclerView);
rv.setLayoutManager(新直线布局经理(本));
rv.setAdapter(新的RecyclerView.Adapter(){
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int位置){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.list_项,parent,false);
返回新的ViewHolder(视图);}
@凌驾
BindViewHolder上的公共无效(RecyclerView.ViewHolder ViewHolder,int位置){
viewHolder.text1.setText(“培根”);
viewHolder.text2.setText(“培根益普生肉丸或艾美肉丸凯文排骨、法兰克福猪腌牛肉肉糕、条状牛排”);
}
@凌驾
public int getItemCount(){
返回30;
}
});
}//在创建方法结束时
私有静态类ViewHolder扩展了RecyclerView.ViewHolder{
TextView text1;
TextView text2;
公共视图持有者(视图项视图){
超级(项目视图);
text1=itemView.findviewbyd(android.R.id.text1);
text2=itemView.findviewbyd(android.R.id.text2);
}
}
}

我认为一件事可能是您将ViewHolder保留在RecycleServiceAdapter类之外。将ViewHolder放在其中,或者尝试将两个文本视图都保持为公共


在输入了所有这些之后,我意识到它不能回答你的问题,所以我将把它放在下面,以防你以后想参考它

所以它不会按你的方式工作。您需要有一个包含对象的ArrayList,每个对象都包含放置内容的信息。我将添加代码以更好地解释:

在您的示例中,每个单元格只包含一个TextView,因此创建一个模型类,并根据需要命名它

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;
   }
}
此模型将包含要显示的信息。现在在RecyclerView类中创建ArrayList:

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

//您也可以在构造函数中初始化。
私有ArrayList cellsList=新ArrayList();
//设置ArrayList
公共无效集合列表(ArrayList列表){
cellsList=列表;
notifyDataSetChanged();
}

最后,在onBindViewHolder方法中,使用ArrayList中的项目设置每个单元格的属性。

请在postYou's welcome中添加代码!