当稍后初始化要传递的变量时,如何在 onCreate 中设置回收器视图适配器?
How to set a recycler view adapter in onCreate when the variable to passed is initalised later?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= findViewById(R.id.recyclerview_student);
final StudentAdapter adapter= new StudentAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
buttonAddTask=findViewById(R.id.Fbutton_add);
buttonAddTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, addStudent.class);
startActivity(intent);
}
});
getStudents();
}
这是主要的activity
@Override
protected void onPostExecute(List<Student> student) {
super.onPostExecute(student);
Log.e("size","student adapter check");
StudentAdapter adapter= new StudentAdapter(MainActivity.this, student);
Log.e("adapter","NULL check");
recyclerView.setAdapter(adapter);
}
这是onPostExecute
public StudentAdapter(Context mCtx, List<Student> studentList) {
this.mCtx = mCtx;
this.studentList = studentList;
}
这是适配器的构造函数
我想做的是从 onCreate 初始化这个构造函数。但我不能这样做,因为列表一开始是空的。因为在那之后我在一个使用异步任务的函数中获取列表的数据(是一个房间数据库)。但是,如果我在 onPost() 中设置适配器,应用程序将无法运行,显示适配器未设置。
像这样在适配器中创建一个函数
public void setData(List<Student> studentList){
this.studentList=studentList;
notifyDataSetChanged();
}
之后,一旦你从数据库中获取数据,就可以设置类似的
adapter.setData(studentList);
这就是你需要做的。
在 onCreate 中使用空 arrayList(非空)创建适配器并与回收器视图绑定。
每当你有来自数据库的新列表或API通过调用刷新列表的方法刷新适配器中的列表时,不要创建多个适配器对象。
在适配器中
public void swapData(List<Student> filtersList) {
if (filtersList != null) {
this.arrayList.clear();
this.arrayList.addAll(filtersList);
notifyDataSetChanged();
}
}
当您在 activity 中刷新数据集时调用此方法:
protected void onPostExecute(List<Student> student) {
super.onPostExecute(student);
Log.e("size","student adapter check");
adapter.swapData(student);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= findViewById(R.id.recyclerview_student);
final StudentAdapter adapter= new StudentAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
buttonAddTask=findViewById(R.id.Fbutton_add);
buttonAddTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, addStudent.class);
startActivity(intent);
}
});
getStudents();
}
这是主要的activity
@Override
protected void onPostExecute(List<Student> student) {
super.onPostExecute(student);
Log.e("size","student adapter check");
StudentAdapter adapter= new StudentAdapter(MainActivity.this, student);
Log.e("adapter","NULL check");
recyclerView.setAdapter(adapter);
}
这是onPostExecute
public StudentAdapter(Context mCtx, List<Student> studentList) {
this.mCtx = mCtx;
this.studentList = studentList;
}
这是适配器的构造函数
我想做的是从 onCreate 初始化这个构造函数。但我不能这样做,因为列表一开始是空的。因为在那之后我在一个使用异步任务的函数中获取列表的数据(是一个房间数据库)。但是,如果我在 onPost() 中设置适配器,应用程序将无法运行,显示适配器未设置。
像这样在适配器中创建一个函数
public void setData(List<Student> studentList){
this.studentList=studentList;
notifyDataSetChanged();
}
之后,一旦你从数据库中获取数据,就可以设置类似的
adapter.setData(studentList);
这就是你需要做的。
在 onCreate 中使用空 arrayList(非空)创建适配器并与回收器视图绑定。
每当你有来自数据库的新列表或API通过调用刷新列表的方法刷新适配器中的列表时,不要创建多个适配器对象。
在适配器中
public void swapData(List<Student> filtersList) {
if (filtersList != null) {
this.arrayList.clear();
this.arrayList.addAll(filtersList);
notifyDataSetChanged();
}
}
当您在 activity 中刷新数据集时调用此方法:
protected void onPostExecute(List<Student> student) {
super.onPostExecute(student);
Log.e("size","student adapter check");
adapter.swapData(student);
}