我试图将数据填充到RV。如果我使用自定义适配器(RV的传统适配器)。
数据正在填充。但我想使用FirebaseRecyclerAdapter
。这样我就可以减少一些Boiler Plate代码。实现时我得到Failed to bounce to type
错误。
来自Firebase的JSON
{
"subscriptions" : {
"obama@gmsil,com" : {
"-KDnfjROKeFAL9wccsxY" : {
"category" : "Mobility",
"goal" : "afternoon body weight workout",
"length" : 1,
"title" : "Afternoon HiiT",
"weeks" : {
"week1" : [ "High Knees", "Squats", "Lunges", "Diamond Push-ups", "Lying Triceps Lifts" ]
}
}
}
}
}
Program.java
public class Program {
private String title;
private String goal;
private String category;
private int length;
HashMap<String, ArrayList<String>> weeks;
public Program() {
}
public Program(String title, String goal, String category, int length, HashMap<String, ArrayList<String>> weeks) {
this.title = title;
this.goal = goal;
this.category = category;
this.length = length;
this.weeks = weeks;
}
public String getTitle() {
return title;
}
public String getGoal() {
return goal;
}
public String getCategory() {
return category;
}
public int getLength() {
return length;
}
public HashMap<String, ArrayList<String>> getweeks() {
return weeks;
}
}
片段代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_latest, container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new FirebaseRecyclerAdapter<Program, ProgramVH>(Program.class, R.layout.program_item, ProgramVH.class, mRef) {
@Override
public void populateViewHolder(final ProgramVH programViewHolder, Program mProgram, int position) {
programViewHolder.setName(program.getTitle());
programViewHolder.level.setText(program.getLength());
}
};
mRecyclerView.setAdapter(mAdapter);
FetchData();
return v;
}
public void FetchData(){
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot programSnapshot : userSnapshot.getChildren()) {
program = programSnapshot.getValue(Program.class);
Log.e("VH", program.getTitle()); // Title is getting printed
}
}
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
mAdapter.cleanup();
}
ViewHolder
public static class ProgramVH extends RecyclerView.ViewHolder {
protected ImageView header;
protected TextView title;
protected TextView level;
public ProgramVH(View view) {
super(view);
this.header = (ImageView) view.findViewById(R.id.coverImage);
this.title = (TextView) view.findViewById(R.id.title);
this.level = (TextView) view.findViewById(R.id.level);
}
public void setName(String name) {
title.setText(name);
}
}
STACK TRACE
08:55:19.045 5717-5717 / com.android.sam E / VH:Afternoon HiiT 03-30 08:55:19.045 5717-5717 / com.android.sam D / AndroidRuntime:关闭VM03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:致命异常:主要 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:FATAL EXCEPTION:main 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:进程:com.android.sam,PID:5717 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:com.firebase.client.FirebaseException:无法退回到类型 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:5465) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:5498) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4735) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4611) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:1988) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3026) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2903) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.View.layout(View.java:15686) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.ViewGroup.layout(ViewGroup.java:5039) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1087) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:797) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.ViewOffsetBehavior.layoutChild(ViewOffsetBehavior.java:63) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:126) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1139) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:810) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.View.layout(View.java:15686) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.ViewGroup.layout(ViewGroup.java:5039) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v4.view.ViewPager.onLayout(ViewPager.java:1684) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.View.layout(View.java:15686) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.ViewGroup.layout(ViewGroup.java:5039) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1139) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:810) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.View.layout(View.java:15686) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.ViewGroup.layout(ViewGroup.java:5039) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.widget.FrameLayout.onLayout(FrameLayout.java:514) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.View.layout(View.java:15686) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.ViewGroup.layout(ViewGroup.java:5039) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.View.layout(View.java:15686) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.ViewGroup.layout(ViewGroup.java:5039) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1183) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.View.layout(View.java:15686) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.ViewGroup.layout(ViewGroup.java:5039) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.widget.FrameLayout.onLayout(FrameLayout.java:514) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.View.layout(View.java:15686) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.ViewGroup.layout(ViewGroup.java:5039) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 03-30 08:55:19.045 5717-5717 / com.android.sam E / AndroidRuntime:在android.view.View
更新
如果我使用以下ref
Firebase mRef = new Firebase("https://arulnadhan.firebaseio.com/").child("subscriptions/obama@gmsil,com");
但是当我使用下面的ref
&amp;时,它不起作用。 FetchData()方法!
Firebase mRef = new Firebase("https://arulnadhan.firebaseio.com/subscriptions");
public void FetchData(){
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot programSnapshot : userSnapshot.getChildren()) {
program = programSnapshot.getValue(Program.class);
Log.e("VH", program.getTitle()); // Title is getting printed
}
}
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
**订阅节点**下的JSON
{
"arulndhan@gmail,com" : {
"-KDnhRwHjssOejrqyenP" : {
"category" : "Strength",
"goal" : "This workout can be done while on the phone!",
"length" : 1,
"title" : "Hello Workouts",
"weeks" : {
"week1" : [ "High Knees", "Jumping Jacks", "Wall sit", "Pushups", "Sit-ups", "Step ups", "Squats", "Tricep dips on chair", "Plank", "High Knees running in place", "Lunges", "Pushup and rotation", "Side plank (alternate per round)", "Alternating Push-Up Plank", "Chest Expander", "Diamond Push-ups", "Dive Bomber Push-ups", "Butt Kickers", "Lying Triceps Lifts", "One Arm Side Push-up", "Overhead Arm Clap", "Overhead Press", "Power Circles", "Push-up and Rotation", "T Push-ups", "Reverse Plank", "Spiderman Push-up", "T Raise", "Tricep Dips", "Wall Push-ups", "Wide Arm Push-ups", "Burpees" ]
}
},
"-KDni3TN4NMyGXePyp92" : {
"category" : "Strength",
"goal" : "This workout can be done by a BABUJI",
"length" : 1,
"title" : "Indian Workouts",
"weeks" : {
"week1" : [ "Diamond Pushups", "Jackknives", "Plyo Lunges", "Plyo Squats", "Single leg plank (alternate per round)", "Plyo Lunges", "Pushup and rotation", "Weighted side plank (alternate per round)", "Alternating Push-Up Plank", "Chest Expander", "Diamond Push-ups", "Dive Bomber Push-ups", "One Arm Side Push-up", "Overhead Press", "Push-up and Rotation", "T Push-ups", "Spiderman Push-up", "Wide Arm Push-ups", "Burpee Pushups" ]
}
}
},
"obama@gmsil,com" : {
"-KDnfjROKeFAL9wccsxY" : {
"category" : "Mobility",
"goal" : "afternoon body weight workout",
"length" : 1,
"title" : "Afternoon HiiT",
"weeks" : {
"week1" : [ "High Knees", "Squats", "Lunges", "Diamond Push-ups", "Lying Triceps Lifts" ]
}
},
"-KDps90Dn6XtJc6Co00b" : {
"category" : "Strength",
"goal" : "goal",
"length" : 1,
"title" : "title",
"weeks" : {
"week1" : [ "Diamond Pushups", "Jackknives", "Plyo Lunges", "Plyo Squats", "Single leg plank (alternate per round)", "Plyo Lunges", "Pushup and rotation", "Weighted side plank (alternate per round)", "Alternating Push-Up Plank", "Chest Expander", "Diamond Push-ups", "Dive Bomber Push-ups", "One Arm Side Push-up", "Overhead Press", "Push-up and Rotation", "T Push-ups", "Spiderman Push-up", "Wide Arm Push-ups", "Burpee Pushups" ]
}
}
}
}
答案 0 :(得分:0)
我不确定你的mRef
变量是指什么,但这就是我需要的全部内容:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RecyclerView view = new RecyclerView(this);
setContentView(view);
view.setLayoutManager(new LinearLayoutManager(this));
Firebase.setAndroidContext(this);
Firebase ref = new Firebase("https://stackoverflow.firebaseio.com/36299197").child("subscriptions/obama@gmsil,com");
FirebaseRecyclerAdapter<Program, ProgramVH> adapter = new FirebaseRecyclerAdapter<Program, ProgramVH>(Program.class, android.R.layout.two_line_list_item, ProgramVH.class, ref) {
@Override
public void populateViewHolder(final ProgramVH programViewHolder, Program program, int position) {
System.out.println("populateViewHolder for position "+position+" with program "+program);
programViewHolder.title.setText(program.getTitle());
programViewHolder.level.setText(""+program.getLength()); // coerce to string to prevent android.content.res.Resources$NotFoundException
}
};
view.setAdapter(adapter);
}
由于我使用了库存布局,我将视图支架稍微修改为:
public static class ProgramVH extends RecyclerView.ViewHolder {
protected TextView title;
protected TextView level;
public ProgramVH(View view) {
super(view);
this.title = (TextView) view.findViewById(android.R.id.text1);
this.level = (TextView) view.findViewById(android.R.id.text2);
}
}
我为Activity36299197
添加了sample repo。如果您启动应用程序并从初始列表中选择Activity36299197,您将看到您发布的JSON中的数据。