Why I can't get some data from Firebase when I use dataSnapshot












1

















I struggle to get data from Firebase for Android.

Please see my code.



        @Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Logger.log("onChildAdded:" + previousChildName);
// Logger.log( dataSnapshot.child("topicid").getValue(String.class));

//dataSnapshot.child("topicid").getValue();
mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);


}


Here is ArticleComment class



public class ArticleComment {
private Date mCommentTime;
private String mComment;
private String mUID;
private String mName;
private int mColorRed;
private int mColorGreen;
private int mColorBlue;


public ArticleComment(){

}
public ArticleComment(Date time, String comment,String name,String uid,int color){
this.mCommentTime = time;
this.mComment = comment;
this.mName = name;
this.mUID = uid;
this.mColorRed=color[0];
this.mColorGreen=color[1];
this.mColorBlue=color[2];

}
public Date getTime(){
return mCommentTime;
}
public void setTime(Date time){
mCommentTime =time;
}
public String getComment(){
return mComment;
}
public void setComment(String comment){
mComment =comment;
}

public String getName(){

return mName;
}
public void setName(String name){
mName=name;
}
public String getUID(){
return mUID;
}
public void setUID(String uid){
mUID=uid;
}
public int getColorRed(){
return mColorRed;
}
public int getColorGreen(){
return mColorGreen;
}
public int getColorBlue(){
return mColorBlue;
}
public void setColor(int red,int green, int blue)
{
mColorRed=red;
mColorGreen=green;
mColorBlue=blue;
}

@Exclude
public Map<String, Object> toMap(){
HashMap<String, Object> hashmap = new HashMap<>();
hashmap.put("time", mCommentTime);
hashmap.put("UID", mUID);
hashmap.put("name", mName);
hashmap.put("comment", mComment);
hashmap.put("colorRed", mColorRed);
hashmap.put("colorBlue", mColorBlue);
hashmap.put("colorGreen", mColorGreen);
return hashmap;
}

}


And here is my DB information.
enter image description here



I could get only


mCommentTime;

mComment;

mName;


But I can't get


mUID;

mColorRed;

mColorGreen;

mColorBlue;

Is there something wrong with my code?

Actually datasnapshot has data but it didn't copy to mComment
enter image description here






Hi! Thank you, friends, gave me feedback here is all code.



public class CommentsActivity extends AppCompatActivity {
private DatabaseReference myRef;
private DatabaseReference mTopicRef;
private RecyclerView mRecyclerView;
private RecyclerArticleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<ArticleComment> myDataset = new ArrayList<ArticleComment>();
private ArticleComment mComment;
private Topic mTopic;
private EditText mTitleEditText;
private String mUserName;
private String mUid;
private int mRedColor=100;
private int mBlueColor=100;
private int mGreenolor=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
//Add action button

Intent intent =getIntent();
String id = intent.getStringExtra("ID");
String date = intent.getStringExtra("Date");
String title = intent.getStringExtra("Title");
String topic = intent.getStringExtra("Topic");
mUserName = intent.getStringExtra("UserName");
mUid = intent.getStringExtra("UID");

SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);
mRedColor= pref.getInt("ColorRed",255);
mGreenolor=pref.getInt("ColorGreen",255);
mBlueColor=pref.getInt("ColorBlue",255);
mRecyclerView = findViewById(R.id.recyclerView_article);
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
//Reference DB
FirebaseDatabase database = FirebaseDatabase.getInstance();
myRef = database.getReference("Main").child("Comments").child(id);
mTopicRef = database.getReference("Main").child("Topics").child(id);

List<String> topicinfo=new ArrayList<String >();
topicinfo.add(date);
topicinfo.add(title);
topicinfo.add(topic);
// Set TestAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);
mAdapter = new RecyclerArticleAdapter(myDataset,topicinfo){
/* @Override
protected void onCheckedChangedRecycle(CompoundButton comButton, final boolean isChecked){
mTopicRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Topic t = mutableData.getValue(Topic.class);
String id = mutableData.child("topicid").getValue(String.class);
t.setTopicID(id);
if (t == null) {
return Transaction.success(mutableData);
}

if (isChecked==true) {
// Unstar the post and remove self from stars
t.setRate(t.getRate()+1);
} else {
// Star the post and add self to stars
t.setRate(t.getRate()-1);
}

// Set value and report transaction success
mutableData.setValue(t);
return Transaction.success(mutableData);
}

@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed

}
});
} */
};

mRecyclerView.setAdapter(mAdapter);

findViewById(R.id.button2).setOnClickListener(button1ClickListener);
mTitleEditText = findViewById(R.id.editText2);
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {

mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);

Logger.log("onChildAdded:addItem" + previousChildName);

mAdapter.addItem(mAdapter.getItemCount()-1, mComment);
Logger.log("onChildAdded:scrollToPosition" + previousChildName);
mAdapter.updateItem(mAdapter.getItemCount()-1,mComment);
mLayoutManager.scrollToPosition(mAdapter.getItemCount()-1);

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {


}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {


}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {


}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};
myRef.addChildEventListener(childEventListener);
}
View.OnClickListener button1ClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// finish();
Logger.log("onClick");
if(mTitleEditText.getText().toString().equals("")){
return;
}
//setting color
int color=new int[3];
color[0]= mRedColor;
color[1]=mGreenolor;
color[2]=mBlueColor;
mComment = new ArticleComment(new Date(),mTitleEditText.getText().toString(),mUserName,mUid,color);
sendTopic(mComment,myRef);
//Delete all text
mTitleEditText.setText("");
}
};

// Sending topic to DB
public void sendTopic(ArticleComment test,DatabaseReference ref) {
String key = ref.push().getKey();
Map<String, Object> map = new HashMap<>();
map.put(key, test.toMap());
ref.updateChildren(map);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return true;
}


Is it enough information?










share|improve this question

























  • can you show where are setting db reference?

    – Zaid Mirza
    Jan 2 at 5:04











  • Are you surely passing these values or not? Check at once carefully and print in console.

    – RAHUL
    Jan 2 at 5:54











  • please show the code where you are building the query to get data.

    – Umar Hussain
    Jan 2 at 6:47











  • Hi! Thank you, I update all code, please see it.

    – tarou yamad
    Jan 2 at 14:00











  • OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day! –

    – tarou yamad
    Jan 3 at 13:46
















1

















I struggle to get data from Firebase for Android.

Please see my code.



        @Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Logger.log("onChildAdded:" + previousChildName);
// Logger.log( dataSnapshot.child("topicid").getValue(String.class));

//dataSnapshot.child("topicid").getValue();
mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);


}


Here is ArticleComment class



public class ArticleComment {
private Date mCommentTime;
private String mComment;
private String mUID;
private String mName;
private int mColorRed;
private int mColorGreen;
private int mColorBlue;


public ArticleComment(){

}
public ArticleComment(Date time, String comment,String name,String uid,int color){
this.mCommentTime = time;
this.mComment = comment;
this.mName = name;
this.mUID = uid;
this.mColorRed=color[0];
this.mColorGreen=color[1];
this.mColorBlue=color[2];

}
public Date getTime(){
return mCommentTime;
}
public void setTime(Date time){
mCommentTime =time;
}
public String getComment(){
return mComment;
}
public void setComment(String comment){
mComment =comment;
}

public String getName(){

return mName;
}
public void setName(String name){
mName=name;
}
public String getUID(){
return mUID;
}
public void setUID(String uid){
mUID=uid;
}
public int getColorRed(){
return mColorRed;
}
public int getColorGreen(){
return mColorGreen;
}
public int getColorBlue(){
return mColorBlue;
}
public void setColor(int red,int green, int blue)
{
mColorRed=red;
mColorGreen=green;
mColorBlue=blue;
}

@Exclude
public Map<String, Object> toMap(){
HashMap<String, Object> hashmap = new HashMap<>();
hashmap.put("time", mCommentTime);
hashmap.put("UID", mUID);
hashmap.put("name", mName);
hashmap.put("comment", mComment);
hashmap.put("colorRed", mColorRed);
hashmap.put("colorBlue", mColorBlue);
hashmap.put("colorGreen", mColorGreen);
return hashmap;
}

}


And here is my DB information.
enter image description here



I could get only


mCommentTime;

mComment;

mName;


But I can't get


mUID;

mColorRed;

mColorGreen;

mColorBlue;

Is there something wrong with my code?

Actually datasnapshot has data but it didn't copy to mComment
enter image description here






Hi! Thank you, friends, gave me feedback here is all code.



public class CommentsActivity extends AppCompatActivity {
private DatabaseReference myRef;
private DatabaseReference mTopicRef;
private RecyclerView mRecyclerView;
private RecyclerArticleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<ArticleComment> myDataset = new ArrayList<ArticleComment>();
private ArticleComment mComment;
private Topic mTopic;
private EditText mTitleEditText;
private String mUserName;
private String mUid;
private int mRedColor=100;
private int mBlueColor=100;
private int mGreenolor=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
//Add action button

Intent intent =getIntent();
String id = intent.getStringExtra("ID");
String date = intent.getStringExtra("Date");
String title = intent.getStringExtra("Title");
String topic = intent.getStringExtra("Topic");
mUserName = intent.getStringExtra("UserName");
mUid = intent.getStringExtra("UID");

SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);
mRedColor= pref.getInt("ColorRed",255);
mGreenolor=pref.getInt("ColorGreen",255);
mBlueColor=pref.getInt("ColorBlue",255);
mRecyclerView = findViewById(R.id.recyclerView_article);
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
//Reference DB
FirebaseDatabase database = FirebaseDatabase.getInstance();
myRef = database.getReference("Main").child("Comments").child(id);
mTopicRef = database.getReference("Main").child("Topics").child(id);

List<String> topicinfo=new ArrayList<String >();
topicinfo.add(date);
topicinfo.add(title);
topicinfo.add(topic);
// Set TestAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);
mAdapter = new RecyclerArticleAdapter(myDataset,topicinfo){
/* @Override
protected void onCheckedChangedRecycle(CompoundButton comButton, final boolean isChecked){
mTopicRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Topic t = mutableData.getValue(Topic.class);
String id = mutableData.child("topicid").getValue(String.class);
t.setTopicID(id);
if (t == null) {
return Transaction.success(mutableData);
}

if (isChecked==true) {
// Unstar the post and remove self from stars
t.setRate(t.getRate()+1);
} else {
// Star the post and add self to stars
t.setRate(t.getRate()-1);
}

// Set value and report transaction success
mutableData.setValue(t);
return Transaction.success(mutableData);
}

@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed

}
});
} */
};

mRecyclerView.setAdapter(mAdapter);

findViewById(R.id.button2).setOnClickListener(button1ClickListener);
mTitleEditText = findViewById(R.id.editText2);
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {

mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);

Logger.log("onChildAdded:addItem" + previousChildName);

mAdapter.addItem(mAdapter.getItemCount()-1, mComment);
Logger.log("onChildAdded:scrollToPosition" + previousChildName);
mAdapter.updateItem(mAdapter.getItemCount()-1,mComment);
mLayoutManager.scrollToPosition(mAdapter.getItemCount()-1);

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {


}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {


}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {


}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};
myRef.addChildEventListener(childEventListener);
}
View.OnClickListener button1ClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// finish();
Logger.log("onClick");
if(mTitleEditText.getText().toString().equals("")){
return;
}
//setting color
int color=new int[3];
color[0]= mRedColor;
color[1]=mGreenolor;
color[2]=mBlueColor;
mComment = new ArticleComment(new Date(),mTitleEditText.getText().toString(),mUserName,mUid,color);
sendTopic(mComment,myRef);
//Delete all text
mTitleEditText.setText("");
}
};

// Sending topic to DB
public void sendTopic(ArticleComment test,DatabaseReference ref) {
String key = ref.push().getKey();
Map<String, Object> map = new HashMap<>();
map.put(key, test.toMap());
ref.updateChildren(map);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return true;
}


Is it enough information?










share|improve this question

























  • can you show where are setting db reference?

    – Zaid Mirza
    Jan 2 at 5:04











  • Are you surely passing these values or not? Check at once carefully and print in console.

    – RAHUL
    Jan 2 at 5:54











  • please show the code where you are building the query to get data.

    – Umar Hussain
    Jan 2 at 6:47











  • Hi! Thank you, I update all code, please see it.

    – tarou yamad
    Jan 2 at 14:00











  • OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day! –

    – tarou yamad
    Jan 3 at 13:46














1












1








1


1








I struggle to get data from Firebase for Android.

Please see my code.



        @Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Logger.log("onChildAdded:" + previousChildName);
// Logger.log( dataSnapshot.child("topicid").getValue(String.class));

//dataSnapshot.child("topicid").getValue();
mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);


}


Here is ArticleComment class



public class ArticleComment {
private Date mCommentTime;
private String mComment;
private String mUID;
private String mName;
private int mColorRed;
private int mColorGreen;
private int mColorBlue;


public ArticleComment(){

}
public ArticleComment(Date time, String comment,String name,String uid,int color){
this.mCommentTime = time;
this.mComment = comment;
this.mName = name;
this.mUID = uid;
this.mColorRed=color[0];
this.mColorGreen=color[1];
this.mColorBlue=color[2];

}
public Date getTime(){
return mCommentTime;
}
public void setTime(Date time){
mCommentTime =time;
}
public String getComment(){
return mComment;
}
public void setComment(String comment){
mComment =comment;
}

public String getName(){

return mName;
}
public void setName(String name){
mName=name;
}
public String getUID(){
return mUID;
}
public void setUID(String uid){
mUID=uid;
}
public int getColorRed(){
return mColorRed;
}
public int getColorGreen(){
return mColorGreen;
}
public int getColorBlue(){
return mColorBlue;
}
public void setColor(int red,int green, int blue)
{
mColorRed=red;
mColorGreen=green;
mColorBlue=blue;
}

@Exclude
public Map<String, Object> toMap(){
HashMap<String, Object> hashmap = new HashMap<>();
hashmap.put("time", mCommentTime);
hashmap.put("UID", mUID);
hashmap.put("name", mName);
hashmap.put("comment", mComment);
hashmap.put("colorRed", mColorRed);
hashmap.put("colorBlue", mColorBlue);
hashmap.put("colorGreen", mColorGreen);
return hashmap;
}

}


And here is my DB information.
enter image description here



I could get only


mCommentTime;

mComment;

mName;


But I can't get


mUID;

mColorRed;

mColorGreen;

mColorBlue;

Is there something wrong with my code?

Actually datasnapshot has data but it didn't copy to mComment
enter image description here






Hi! Thank you, friends, gave me feedback here is all code.



public class CommentsActivity extends AppCompatActivity {
private DatabaseReference myRef;
private DatabaseReference mTopicRef;
private RecyclerView mRecyclerView;
private RecyclerArticleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<ArticleComment> myDataset = new ArrayList<ArticleComment>();
private ArticleComment mComment;
private Topic mTopic;
private EditText mTitleEditText;
private String mUserName;
private String mUid;
private int mRedColor=100;
private int mBlueColor=100;
private int mGreenolor=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
//Add action button

Intent intent =getIntent();
String id = intent.getStringExtra("ID");
String date = intent.getStringExtra("Date");
String title = intent.getStringExtra("Title");
String topic = intent.getStringExtra("Topic");
mUserName = intent.getStringExtra("UserName");
mUid = intent.getStringExtra("UID");

SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);
mRedColor= pref.getInt("ColorRed",255);
mGreenolor=pref.getInt("ColorGreen",255);
mBlueColor=pref.getInt("ColorBlue",255);
mRecyclerView = findViewById(R.id.recyclerView_article);
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
//Reference DB
FirebaseDatabase database = FirebaseDatabase.getInstance();
myRef = database.getReference("Main").child("Comments").child(id);
mTopicRef = database.getReference("Main").child("Topics").child(id);

List<String> topicinfo=new ArrayList<String >();
topicinfo.add(date);
topicinfo.add(title);
topicinfo.add(topic);
// Set TestAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);
mAdapter = new RecyclerArticleAdapter(myDataset,topicinfo){
/* @Override
protected void onCheckedChangedRecycle(CompoundButton comButton, final boolean isChecked){
mTopicRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Topic t = mutableData.getValue(Topic.class);
String id = mutableData.child("topicid").getValue(String.class);
t.setTopicID(id);
if (t == null) {
return Transaction.success(mutableData);
}

if (isChecked==true) {
// Unstar the post and remove self from stars
t.setRate(t.getRate()+1);
} else {
// Star the post and add self to stars
t.setRate(t.getRate()-1);
}

// Set value and report transaction success
mutableData.setValue(t);
return Transaction.success(mutableData);
}

@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed

}
});
} */
};

mRecyclerView.setAdapter(mAdapter);

findViewById(R.id.button2).setOnClickListener(button1ClickListener);
mTitleEditText = findViewById(R.id.editText2);
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {

mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);

Logger.log("onChildAdded:addItem" + previousChildName);

mAdapter.addItem(mAdapter.getItemCount()-1, mComment);
Logger.log("onChildAdded:scrollToPosition" + previousChildName);
mAdapter.updateItem(mAdapter.getItemCount()-1,mComment);
mLayoutManager.scrollToPosition(mAdapter.getItemCount()-1);

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {


}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {


}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {


}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};
myRef.addChildEventListener(childEventListener);
}
View.OnClickListener button1ClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// finish();
Logger.log("onClick");
if(mTitleEditText.getText().toString().equals("")){
return;
}
//setting color
int color=new int[3];
color[0]= mRedColor;
color[1]=mGreenolor;
color[2]=mBlueColor;
mComment = new ArticleComment(new Date(),mTitleEditText.getText().toString(),mUserName,mUid,color);
sendTopic(mComment,myRef);
//Delete all text
mTitleEditText.setText("");
}
};

// Sending topic to DB
public void sendTopic(ArticleComment test,DatabaseReference ref) {
String key = ref.push().getKey();
Map<String, Object> map = new HashMap<>();
map.put(key, test.toMap());
ref.updateChildren(map);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return true;
}


Is it enough information?










share|improve this question


















I struggle to get data from Firebase for Android.

Please see my code.



        @Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Logger.log("onChildAdded:" + previousChildName);
// Logger.log( dataSnapshot.child("topicid").getValue(String.class));

//dataSnapshot.child("topicid").getValue();
mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);


}


Here is ArticleComment class



public class ArticleComment {
private Date mCommentTime;
private String mComment;
private String mUID;
private String mName;
private int mColorRed;
private int mColorGreen;
private int mColorBlue;


public ArticleComment(){

}
public ArticleComment(Date time, String comment,String name,String uid,int color){
this.mCommentTime = time;
this.mComment = comment;
this.mName = name;
this.mUID = uid;
this.mColorRed=color[0];
this.mColorGreen=color[1];
this.mColorBlue=color[2];

}
public Date getTime(){
return mCommentTime;
}
public void setTime(Date time){
mCommentTime =time;
}
public String getComment(){
return mComment;
}
public void setComment(String comment){
mComment =comment;
}

public String getName(){

return mName;
}
public void setName(String name){
mName=name;
}
public String getUID(){
return mUID;
}
public void setUID(String uid){
mUID=uid;
}
public int getColorRed(){
return mColorRed;
}
public int getColorGreen(){
return mColorGreen;
}
public int getColorBlue(){
return mColorBlue;
}
public void setColor(int red,int green, int blue)
{
mColorRed=red;
mColorGreen=green;
mColorBlue=blue;
}

@Exclude
public Map<String, Object> toMap(){
HashMap<String, Object> hashmap = new HashMap<>();
hashmap.put("time", mCommentTime);
hashmap.put("UID", mUID);
hashmap.put("name", mName);
hashmap.put("comment", mComment);
hashmap.put("colorRed", mColorRed);
hashmap.put("colorBlue", mColorBlue);
hashmap.put("colorGreen", mColorGreen);
return hashmap;
}

}


And here is my DB information.
enter image description here



I could get only


mCommentTime;

mComment;

mName;


But I can't get


mUID;

mColorRed;

mColorGreen;

mColorBlue;

Is there something wrong with my code?

Actually datasnapshot has data but it didn't copy to mComment
enter image description here






Hi! Thank you, friends, gave me feedback here is all code.



public class CommentsActivity extends AppCompatActivity {
private DatabaseReference myRef;
private DatabaseReference mTopicRef;
private RecyclerView mRecyclerView;
private RecyclerArticleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<ArticleComment> myDataset = new ArrayList<ArticleComment>();
private ArticleComment mComment;
private Topic mTopic;
private EditText mTitleEditText;
private String mUserName;
private String mUid;
private int mRedColor=100;
private int mBlueColor=100;
private int mGreenolor=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
//Add action button

Intent intent =getIntent();
String id = intent.getStringExtra("ID");
String date = intent.getStringExtra("Date");
String title = intent.getStringExtra("Title");
String topic = intent.getStringExtra("Topic");
mUserName = intent.getStringExtra("UserName");
mUid = intent.getStringExtra("UID");

SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);
mRedColor= pref.getInt("ColorRed",255);
mGreenolor=pref.getInt("ColorGreen",255);
mBlueColor=pref.getInt("ColorBlue",255);
mRecyclerView = findViewById(R.id.recyclerView_article);
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
//Reference DB
FirebaseDatabase database = FirebaseDatabase.getInstance();
myRef = database.getReference("Main").child("Comments").child(id);
mTopicRef = database.getReference("Main").child("Topics").child(id);

List<String> topicinfo=new ArrayList<String >();
topicinfo.add(date);
topicinfo.add(title);
topicinfo.add(topic);
// Set TestAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);
mAdapter = new RecyclerArticleAdapter(myDataset,topicinfo){
/* @Override
protected void onCheckedChangedRecycle(CompoundButton comButton, final boolean isChecked){
mTopicRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Topic t = mutableData.getValue(Topic.class);
String id = mutableData.child("topicid").getValue(String.class);
t.setTopicID(id);
if (t == null) {
return Transaction.success(mutableData);
}

if (isChecked==true) {
// Unstar the post and remove self from stars
t.setRate(t.getRate()+1);
} else {
// Star the post and add self to stars
t.setRate(t.getRate()-1);
}

// Set value and report transaction success
mutableData.setValue(t);
return Transaction.success(mutableData);
}

@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed

}
});
} */
};

mRecyclerView.setAdapter(mAdapter);

findViewById(R.id.button2).setOnClickListener(button1ClickListener);
mTitleEditText = findViewById(R.id.editText2);
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {

mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);

Logger.log("onChildAdded:addItem" + previousChildName);

mAdapter.addItem(mAdapter.getItemCount()-1, mComment);
Logger.log("onChildAdded:scrollToPosition" + previousChildName);
mAdapter.updateItem(mAdapter.getItemCount()-1,mComment);
mLayoutManager.scrollToPosition(mAdapter.getItemCount()-1);

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {


}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {


}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {


}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};
myRef.addChildEventListener(childEventListener);
}
View.OnClickListener button1ClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// finish();
Logger.log("onClick");
if(mTitleEditText.getText().toString().equals("")){
return;
}
//setting color
int color=new int[3];
color[0]= mRedColor;
color[1]=mGreenolor;
color[2]=mBlueColor;
mComment = new ArticleComment(new Date(),mTitleEditText.getText().toString(),mUserName,mUid,color);
sendTopic(mComment,myRef);
//Delete all text
mTitleEditText.setText("");
}
};

// Sending topic to DB
public void sendTopic(ArticleComment test,DatabaseReference ref) {
String key = ref.push().getKey();
Map<String, Object> map = new HashMap<>();
map.put(key, test.toMap());
ref.updateChildren(map);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return true;
}


Is it enough information?







java android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 13:59







tarou yamad

















asked Jan 2 at 4:55









tarou yamadtarou yamad

84




84













  • can you show where are setting db reference?

    – Zaid Mirza
    Jan 2 at 5:04











  • Are you surely passing these values or not? Check at once carefully and print in console.

    – RAHUL
    Jan 2 at 5:54











  • please show the code where you are building the query to get data.

    – Umar Hussain
    Jan 2 at 6:47











  • Hi! Thank you, I update all code, please see it.

    – tarou yamad
    Jan 2 at 14:00











  • OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day! –

    – tarou yamad
    Jan 3 at 13:46



















  • can you show where are setting db reference?

    – Zaid Mirza
    Jan 2 at 5:04











  • Are you surely passing these values or not? Check at once carefully and print in console.

    – RAHUL
    Jan 2 at 5:54











  • please show the code where you are building the query to get data.

    – Umar Hussain
    Jan 2 at 6:47











  • Hi! Thank you, I update all code, please see it.

    – tarou yamad
    Jan 2 at 14:00











  • OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day! –

    – tarou yamad
    Jan 3 at 13:46

















can you show where are setting db reference?

– Zaid Mirza
Jan 2 at 5:04





can you show where are setting db reference?

– Zaid Mirza
Jan 2 at 5:04













Are you surely passing these values or not? Check at once carefully and print in console.

– RAHUL
Jan 2 at 5:54





Are you surely passing these values or not? Check at once carefully and print in console.

– RAHUL
Jan 2 at 5:54













please show the code where you are building the query to get data.

– Umar Hussain
Jan 2 at 6:47





please show the code where you are building the query to get data.

– Umar Hussain
Jan 2 at 6:47













Hi! Thank you, I update all code, please see it.

– tarou yamad
Jan 2 at 14:00





Hi! Thank you, I update all code, please see it.

– tarou yamad
Jan 2 at 14:00













OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day! –

– tarou yamad
Jan 3 at 13:46





OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day! –

– tarou yamad
Jan 3 at 13:46












2 Answers
2






active

oldest

votes


















0














Firebase follows JavaBean naming conventions to determine the name of the JSON property from the Java code. And in that convention getUID and setUID map to a property uID with a lowercase u.



To make the Firebase client adopt your naming convention, annotate the getter and setter with a @PropertyName:



@PropertyName("UID")
public String getUID(){
return mUID;
}
@PropertyName("UID")
public void setUID(String uid){
mUID=uid;
}


I'm not immediately sure why the other properties don't work. When this happens, I find it most useful to write an object of the type of Firebase, to see what it generates.






share|improve this answer
























  • OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day!

    – tarou yamad
    Jan 3 at 13:41





















0














**




If you want to store values in a model .Then your variable name should
be same as Firebase node or else you need to typeConvert it .




**



    private Date mCommentTime;
private String mComment;
private String mUID;
private String mName;
private int mColorRed;
private int mColorGreen;
private int mColorBlue;


Replace above with



private int time;
private String comment;
private String UID;
private String name;
private int colorRed;
private int colorBlue;
private int colorGreen;


now make getters,setters,constructor etc. using above nodes. (Now you will be able to get all the values ) .



Hope its gonna help you :)






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001385%2fwhy-i-cant-get-some-data-from-firebase-when-i-use-datasnapshot%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Firebase follows JavaBean naming conventions to determine the name of the JSON property from the Java code. And in that convention getUID and setUID map to a property uID with a lowercase u.



    To make the Firebase client adopt your naming convention, annotate the getter and setter with a @PropertyName:



    @PropertyName("UID")
    public String getUID(){
    return mUID;
    }
    @PropertyName("UID")
    public void setUID(String uid){
    mUID=uid;
    }


    I'm not immediately sure why the other properties don't work. When this happens, I find it most useful to write an object of the type of Firebase, to see what it generates.






    share|improve this answer
























    • OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day!

      – tarou yamad
      Jan 3 at 13:41


















    0














    Firebase follows JavaBean naming conventions to determine the name of the JSON property from the Java code. And in that convention getUID and setUID map to a property uID with a lowercase u.



    To make the Firebase client adopt your naming convention, annotate the getter and setter with a @PropertyName:



    @PropertyName("UID")
    public String getUID(){
    return mUID;
    }
    @PropertyName("UID")
    public void setUID(String uid){
    mUID=uid;
    }


    I'm not immediately sure why the other properties don't work. When this happens, I find it most useful to write an object of the type of Firebase, to see what it generates.






    share|improve this answer
























    • OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day!

      – tarou yamad
      Jan 3 at 13:41
















    0












    0








    0







    Firebase follows JavaBean naming conventions to determine the name of the JSON property from the Java code. And in that convention getUID and setUID map to a property uID with a lowercase u.



    To make the Firebase client adopt your naming convention, annotate the getter and setter with a @PropertyName:



    @PropertyName("UID")
    public String getUID(){
    return mUID;
    }
    @PropertyName("UID")
    public void setUID(String uid){
    mUID=uid;
    }


    I'm not immediately sure why the other properties don't work. When this happens, I find it most useful to write an object of the type of Firebase, to see what it generates.






    share|improve this answer













    Firebase follows JavaBean naming conventions to determine the name of the JSON property from the Java code. And in that convention getUID and setUID map to a property uID with a lowercase u.



    To make the Firebase client adopt your naming convention, annotate the getter and setter with a @PropertyName:



    @PropertyName("UID")
    public String getUID(){
    return mUID;
    }
    @PropertyName("UID")
    public void setUID(String uid){
    mUID=uid;
    }


    I'm not immediately sure why the other properties don't work. When this happens, I find it most useful to write an object of the type of Firebase, to see what it generates.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 15:39









    Frank van PuffelenFrank van Puffelen

    241k29387414




    241k29387414













    • OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day!

      – tarou yamad
      Jan 3 at 13:41





















    • OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day!

      – tarou yamad
      Jan 3 at 13:41



















    OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day!

    – tarou yamad
    Jan 3 at 13:41







    OMG! I can do it! You guys are so genius!! Thank you so much! I really appreciate. Have a nice day!

    – tarou yamad
    Jan 3 at 13:41















    0














    **




    If you want to store values in a model .Then your variable name should
    be same as Firebase node or else you need to typeConvert it .




    **



        private Date mCommentTime;
    private String mComment;
    private String mUID;
    private String mName;
    private int mColorRed;
    private int mColorGreen;
    private int mColorBlue;


    Replace above with



    private int time;
    private String comment;
    private String UID;
    private String name;
    private int colorRed;
    private int colorBlue;
    private int colorGreen;


    now make getters,setters,constructor etc. using above nodes. (Now you will be able to get all the values ) .



    Hope its gonna help you :)






    share|improve this answer




























      0














      **




      If you want to store values in a model .Then your variable name should
      be same as Firebase node or else you need to typeConvert it .




      **



          private Date mCommentTime;
      private String mComment;
      private String mUID;
      private String mName;
      private int mColorRed;
      private int mColorGreen;
      private int mColorBlue;


      Replace above with



      private int time;
      private String comment;
      private String UID;
      private String name;
      private int colorRed;
      private int colorBlue;
      private int colorGreen;


      now make getters,setters,constructor etc. using above nodes. (Now you will be able to get all the values ) .



      Hope its gonna help you :)






      share|improve this answer


























        0












        0








        0







        **




        If you want to store values in a model .Then your variable name should
        be same as Firebase node or else you need to typeConvert it .




        **



            private Date mCommentTime;
        private String mComment;
        private String mUID;
        private String mName;
        private int mColorRed;
        private int mColorGreen;
        private int mColorBlue;


        Replace above with



        private int time;
        private String comment;
        private String UID;
        private String name;
        private int colorRed;
        private int colorBlue;
        private int colorGreen;


        now make getters,setters,constructor etc. using above nodes. (Now you will be able to get all the values ) .



        Hope its gonna help you :)






        share|improve this answer













        **




        If you want to store values in a model .Then your variable name should
        be same as Firebase node or else you need to typeConvert it .




        **



            private Date mCommentTime;
        private String mComment;
        private String mUID;
        private String mName;
        private int mColorRed;
        private int mColorGreen;
        private int mColorBlue;


        Replace above with



        private int time;
        private String comment;
        private String UID;
        private String name;
        private int colorRed;
        private int colorBlue;
        private int colorGreen;


        now make getters,setters,constructor etc. using above nodes. (Now you will be able to get all the values ) .



        Hope its gonna help you :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 15:51









        vinay mishravinay mishra

        2113




        2113






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001385%2fwhy-i-cant-get-some-data-from-firebase-when-i-use-datasnapshot%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules