我的Android FirebaseUI Recycler視圖對String類數據類型響應良好。但是,當我使用POJO類作爲數據類型時,不會填充視圖。FirebaseUI-Android RecyclerView無法響應POJO
我已花時間確保POJO中的名稱與JSON文件中的名稱相匹配。下面是我的代碼:
public class MainActivity extends AppCompatActivity {
public static class MessageViewHolder extends RecyclerView.ViewHolder {
TextView messageTextView;
public MessageViewHolder(View view) {
super(view);
messageTextView =(TextView) messageTextView.findViewById(android.R.id.text1);
}
}
android.support.v7.widget.RecyclerView recyclerView;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart()
{
super.onStart();
//Recycler
recyclerView=(android.support.v7.widget.RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Adapter
FirebaseRecyclerAdapter<User, MessageViewHolder> mFirebaseAdapter=
new FirebaseRecyclerAdapter<User, MessageViewHolder>(User.class,
R.layout.layout,
MessageViewHolder.class,
myRef.child("User")
) {
@Override
protected void populateViewHolder(MessageViewHolder viewHolder, User model, int position) {
viewHolder.messageTextView.setText(model.getName());
}
};
recyclerView.setAdapter(mFirebaseAdapter);
}
}
POJO:
public class User {
public User(){
}
String Name;
String email;
public User(String Name){
this.Name=Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getName() {
return Name;
}
}
錯誤從火力地堡
異常com.google.firebase.database.DatabaseException:無法轉換對象類型java.lang.String類型com.projectgenuis.eneanews.User
EDIT(JSON)
{
"User" : {
"Name" : "Zion",
}
請分享yoru數據庫中'/ User'節點的JSON(文本,沒有截圖)。您可以通過單擊[您的Firebase數據庫控制檯](https://console.firebase.google.com/project/_/database/data/)中的導出JSON鏈接輕鬆獲取此信息。 –
JSON:{ 「用戶」:{ 「名稱」:「錫安」, } –