我遇到了以下问题。当我试图从FirebaseRecyclerAdapter获取值时,我得到null,只键入我能看到的这个。 请查看我的代码:
public abstract class TabFragmentLists extends Fragment {
private DatabaseReference mDatabase;
private FirebaseRecyclerAdapter<Post, PostViewHolder> mAdapter;
private RecyclerView mRecycler;
private LinearLayoutManager mManager;
public TabFragmentLists() {
// Required empty public constructor
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_tab_lists, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference();
mRecycler = (RecyclerView) rootView.findViewById(R.id.messages_list);
mRecycler.setHasFixedSize(true);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Set up Layout Manager, reverse layout
mManager = new LinearLayoutManager(getActivity());
mManager.setReverseLayout(true);
//mManager.setStackFromEnd(true);
mRecycler.setLayoutManager(mManager);
// Set up FirebaseRecyclerAdapter with the Query
Query postsQuery = getQuery(mDatabase);
mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.item_post,
PostViewHolder.class, postsQuery) {
@Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Post model, final int position) {
final DatabaseReference postRef = getRef(position);
// Set click listener for the whole post view
final String postKey = postRef.getKey();
viewHolder.bindToPost(model, new View.OnClickListener() {
@Override
public void onClick(View postView) {
Log.e("OnClick","== Bind===");
}
});
}
};
mRecycler.setAdapter(mAdapter);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mAdapter != null) {
mAdapter.cleanup();
}
}
public String getUid() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
public abstract Query getQuery(DatabaseReference databaseReference);
}
PostViewHolder
public class PostViewHolder extends RecyclerView.ViewHolder {
public TextView messsageView;
public PostViewHolder(View itemView) {
super(itemView);
messsageView = (TextView) itemView.findViewById(R.id.post_message);
}
public void bindToPost(Post post, View.OnClickListener starClickListener) {
//null and crash
Log.e("Post", String.valueOf(post.getMessage()));
messsageView.setText(post.getMessage());
}
}
发布课程
@IgnoreExtraProperties
public class Post {
public String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Post() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public Post(String message) {
this.message = message;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put(PostKeys.kMessage.toString(), message);
return result;
}
}
会欣赏任何提示。感谢
修改
public class TabFragmentPosts extends TabFragmentLists {
public TabFragmentPosts() {
// Required empty public constructor
}
@Override
public Query getQuery(DatabaseReference databaseReference) {
// All my posts
return databaseReference.child("post");
}
}
这正是我最终拥有的
E/LIST dataSnapshot: DataSnapshot { key = -KOgO2hC9Fh_vhe1kh1z, value = null }
答案 0 :(得分:1)
最后,发现了问题 class Post应该具有相同的变量名称(考虑大写/小写)
@IgnoreExtraProperties
public class Post {
public String Message; // HERE IS THE PROBLEM WAS
public Post() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public Post(String Message) {
this.Message = Message;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("Message", Message);
return result;
}
}
感谢所有试图帮助的人!欣赏它!
答案 1 :(得分:0)
首先检查安全规则,无论您是否已启用权限...如果未启用权限...在以下链接中查找如何设置权限... https://firebase.google.com/docs/database/security/quickstart
答案 2 :(得分:0)
除非我误读了您的代码,否则您似乎错误地设置了您的DatabaseReference - 您目前只是将其指向基础Firebase参考,但您需要将其指向您的&#39 ;交&#39;路径。
您需要在mDatabase属性中添加.child(&#34; post&#34;)。