java - 回收站视图无法正常工作且没有任何错误
问题描述
我一直在尝试使用回收器视图在文本视图列表中显示一些数据。但是我的代码不起作用。logcat 中没有错误消息。我过去曾使用过回收站视图,但我不知道问题出在哪里。
package com.example.diffutil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Adaptershow extends RecyclerView.Adapter<Adaptershow.MyViewHolder> {
ArrayList<Student> mStudents;
public Adaptershow(ArrayList<Student> mStudents) {
this.mStudents = mStudents;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.show.setText(mStudents.get(position).getName());
}
@Override
public int getItemCount() {
return mStudents.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView show;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
show = itemView.findViewById(R.id.show);
}
}
}
diffutil 是项目的名称。
下面的 MainActivity 代码:-
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Student> students;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv);
students = new ArrayList<>();
students.add(new Student(1,"a","q"));
students.add(new Student(1,"b","w"));
students.add(new Student(1,"c","e"));
students.add(new Student(1,"d","r"));
students.add(new Student(1,"e","t"));
Adaptershow adapter = new Adaptershow(students);
recyclerView.setAdapter(adapter);
}
}
下面的 XML 代码:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv"/>
</RelativeLayout>
项目 XML 代码:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/show"
android:text="hello"/>
</RelativeLayout>
解决方案
我没有看到LinearLayoutManager
您的回收站视图:
..........
..........
Adaptershow adapter = new Adaptershow(students);
//here
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
推荐阅读
- r - 在循环中将数据附加到数据帧,但只有最后一个值进入数据帧
- java - JPA 列不存在
- wordpress - Nginx - Wordpress 子目录重定向循环
- java - 比较/排序元素,然后使用具有 O(n) 复杂度的数组列表对它们进行平方
- tkinter - 在 Tkinter 中更改按钮
- flutter - Flutter 边缘 onFocus FocusNode
- python - python中的凝聚聚类实现
- amazon-web-services - 如何在 AWS S3 生命周期过期前缀规则中使用正则表达式
- python - 如何指定神经网络层中的连接(在 keras 中)?
- javascript - 将 json 数组插入到 hasura