在RecyclerView中,在OnBindViewHolder方法中:

huangapple go评论77阅读模式
英文:

in RecyclerView, in OnBindViewHolder method

问题

RecyclerView中,OnBindViewHolder方法中,我无法获取位于ViewHolder类中的TextViews。为什么?问题出在哪里?

请参考截屏:
在RecyclerView中,在OnBindViewHolder方法中:

以下是我的代码:

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:
在RecyclerView中,在OnBindViewHolder方法中:

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(&quot;Screen Title&quot;);

        RecyclerView rv = findViewById(R.id.recyclerview);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;() {
            @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(&quot;Bacon&quot;);
                viewHolder.text2.setText(&quot;Bacon ipsum dolor amet pork belly meatball kevin spare ribs. Frankfurter swine corned beef meatloaf, strip steak.&quot;);
            }

            @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&lt;Model&gt; cellsList = new ArrayList&lt;&gt;(); 
//Set the ArrayList
public void setList(ArrayList&lt;Model&gt; list){
cellsList = list;
notifyDataSetChanged();
}

Finally, within the onBindViewHolder method, set the attributes for each cell using the items in the ArrayList.

huangapple
  • 本文由 发表于 2020年9月13日 02:45:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63863767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定