Reputation: 3006
I stuck with the following problem. When I tried to get value from FirebaseRecyclerAdapter I get null, only key the this that I can see. Please review my code:
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());
}
}
Post class
@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;
}
}
Would appreciate any hint. Thanks
EDIT
public class TabFragmentPosts extends TabFragmentLists {
public TabFragmentPosts() {
// Required empty public constructor
}
@Override
public Query getQuery(DatabaseReference databaseReference) {
// All my posts
return databaseReference.child("post");
}
}
This exactly what I eventually have
E/LIST dataSnapshot: DataSnapshot { key = -KOgO2hC9Fh_vhe1kh1z, value = null }
Upvotes: 0
Views: 639
Reputation: 3006
Finally, found the problem class Post should have exact the same name of variables (taking into account uppercase/lowercase)
@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;
}
}
Thanks to all who tried to help!!! Appreciate it!
Upvotes: 1
Reputation: 802
Unless I've misread your code it looks like you've set your DatabaseReference incorrectly - you're currently just pointing it to your base Firebase reference but you need to direct it to your 'post' path.
You'll need to add a .child("post") to your mDatabase property.
Upvotes: 0
Reputation: 532
First check the security rules, whether you have enabled permissions for it or not...If not enable permissions...In the below link find how to set permissions...https://firebase.google.com/docs/database/security/quickstart
Upvotes: 0