有什么方法可以在 Tab 视图中实现 FirebaseListAdapter 吗?
Is there is any way to implement FirebaseListAdapter in a Tab view?
我在 activity 中创建了一个选项卡视图。现在我想在每个选项卡中显示要从 Firebase 检索的列表项。
我可以检索并显示 activity 中的数据。但我不知道如何在片段中使用 FirebaseListAdapter
。 FirebaseListAdapter
需要第一个参数作为 Activity
,但如何将片段作为 Activity 传递?
childRef = databaseReference.child(str);
FirebaseListAdapter<Leave> firebaseListAdapter = new FirebaseListAdapter<Leave>(
this,
Leave.class,
R.layout.leavelistview,
childRef
) {
@Override
protected void populateView(View v, Leave model, int position) {
((TextView)v.findViewById(R.id.lv_staffname)).setText(model.getName());
((TextView)v.findViewById(R.id.lv_type)).setText(model.getType());
((TextView)v.findViewById(R.id.lv_duration)).setText(model.getDuration());
}
};
listView.setAdapter(firebaseListAdapter);
这是我在 activity 中使用的代码。但是在片段中实现它会给我一个错误,因为我们不能为片段传递 this
。
您可以对片段调用 getActivity()
以将其传递给适配器。像这样:
FirebaseListAdapter<Leave> firebaseListAdapter = new FirebaseListAdapter<Leave>(
getActivity(),
Leave.class,
R.layout.leavelistview,
childRef
) {
@Override
protected void populateView(View v, Leave model, int position) {
((TextView)v.findViewById(R.id.lv_staffname)).setText(model.getName());
((TextView)v.findViewById(R.id.lv_type)).setText(model.getType());
((TextView)v.findViewById(R.id.lv_duration)).setText(model.getDuration());
}
};
我在 activity 中创建了一个选项卡视图。现在我想在每个选项卡中显示要从 Firebase 检索的列表项。
我可以检索并显示 activity 中的数据。但我不知道如何在片段中使用 FirebaseListAdapter
。 FirebaseListAdapter
需要第一个参数作为 Activity
,但如何将片段作为 Activity 传递?
childRef = databaseReference.child(str);
FirebaseListAdapter<Leave> firebaseListAdapter = new FirebaseListAdapter<Leave>(
this,
Leave.class,
R.layout.leavelistview,
childRef
) {
@Override
protected void populateView(View v, Leave model, int position) {
((TextView)v.findViewById(R.id.lv_staffname)).setText(model.getName());
((TextView)v.findViewById(R.id.lv_type)).setText(model.getType());
((TextView)v.findViewById(R.id.lv_duration)).setText(model.getDuration());
}
};
listView.setAdapter(firebaseListAdapter);
这是我在 activity 中使用的代码。但是在片段中实现它会给我一个错误,因为我们不能为片段传递 this
。
您可以对片段调用 getActivity()
以将其传递给适配器。像这样:
FirebaseListAdapter<Leave> firebaseListAdapter = new FirebaseListAdapter<Leave>(
getActivity(),
Leave.class,
R.layout.leavelistview,
childRef
) {
@Override
protected void populateView(View v, Leave model, int position) {
((TextView)v.findViewById(R.id.lv_staffname)).setText(model.getName());
((TextView)v.findViewById(R.id.lv_type)).setText(model.getType());
((TextView)v.findViewById(R.id.lv_duration)).setText(model.getDuration());
}
};