我只是分享相关代码:
MainActivity:
RecyclerView recyclerView;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
Firebase.setAndroidContext(this);
databaseReference = FirebaseDatabase.getInstance().getReference();
recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
FirebaseRecyclerAdapter<Grupo,GruposAdapter.ViewHolder> adapter =
new FirebaseRecyclerAdapter<Grupo, GruposAdapter.ViewHolder>(
Grupo.class,
R.layout.item,
GruposAdapter.ViewHolder.class,
databaseReference.child("Grupos")
) {
@Override
protected void populateViewHolder(GruposAdapter.ViewHolder viewHolder, Grupo model, int position) {
viewHolder.id.setText(String.valueOf(model.getId()));
viewHolder.grupo.setText(model.getGroupname());
}
};
recyclerView.setAdapter(adapter);
}
错误:
04-19 14:50:52.886 22120-22120/datos.agregar.firebase.ejemplo.com.ejemploagregardatoscffirebase E/AndroidRuntime: FATAL EXCEPTION: main
Process: datos.agregar.firebase.ejemplo.com.ejemploagregardatoscffirebase, PID: 22120
java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
为什么NoSuchMethodException
?想法?
固定:
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView id,grupo;
public CardView cardview;
public ViewHolder(View itemView){
super(itemView);
id = (TextView) itemView.findViewById(R.id.idgrupo);
grupo = (TextView) itemView.findViewById(R.id.grupo);
cardview = (CardView) itemView.findViewById(R.id.cardview);
}
}
答案 0 :(得分:1)
您的GruposAdapter.ViewHolder
似乎缺少必需的构造函数。见https://github.com/firebase/FirebaseUI-Android/issues/46#issuecomment-167373575。从那里:
这可能有两个原因:
- 确保您的TaskViewHolder类是公开的 *如果你的TaskViewHolder是一个内部类,例如你的活动,确保它是静态的
很可能
public static TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {