I have the above format in firebase realtime database
I need to get data in arraylist
public class EmojiView extends AppCompatActivity {
FirebaseUser firebaseUser;
DatabaseReference reference;
ArrayList<String> images = new ArrayList<>();
private void getImagesfromDB() {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("stickers");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String data = snapshot.getValue(String.class);
images.add(data);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emoji_view);
getImagesfromDB();
}
}
I get the urls in data variable in OnDataChangemethod, but i am not able to assign those values to images arraylist
Try this:
Change your reference to this:
reference = FirebaseDatabase.getInstance().getReference("stickers").getChildren();
And then loop through the snapshots (which should be the children of "stickers") this way:
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot item : snapshot){
stickers.add(item.getValue(String.class));
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
Related
This is my firebase database:
I want to display the value of the auto-generated id given by the push method
this is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_recycler_view);
FirebaseApp.initializeApp(this);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
String key = newRef.getKey();
preRef.child(key).get();
list = new ArrayList<>();
myAdapter = new MyAdapter(this, list);
recyclerView.setAdapter(myAdapter);
preRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Calendar calendar = dataSnapshot.getValue(Calendar.class);
list.add(calendar);
}
myAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
I cant display the value of the Uid can someone please help me?
To display the list of dates, you can do:
DatabaseReference actlogRef = FirebaseDatabase.getInstance().getReference("ActLog");
actlogRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.child("date").getChildren()) {
String date = dataSnapshot.getValue(String.class);
list.add(date);
}
myAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});
For this, list will have to be a list of strings, as that's what you store in the database. If you must use a list of Date objects, have a look at questions about parsing a data from a string in java.
i'm a beginner in android application, could anyone help me and show me how could i retrieve the generated id value node of the current user Id ? please find below my code. Thanks
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference mDb = mDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
final String userKey = user.getUid();
mDb.child("students").child(currentuser).getRef().addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String reference = dataSnapshot.getRef().toString();
String userID = dataSnapshot.child("level").getValue(String.class);
textViewStudentName.setText(reference);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try below to get level:
DatabaseReference fireReference = FirebaseDatabase.getInstance().getReference("students");
fireReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
if(childSnapshot.hasChild(userKey)) {
String userID = childSnapshot.child(userKey).child("level").getValue(String.class);
break;
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
For my chat application, I have saved EditText data to firebase but not I am not able to display the same in ListView. I am able to see the EditText data in Firebase database and I want to retrieve it in a ListView. Anybody can help me?
public class Chatbox extends AppCompatActivity {
private DatabaseReference databaseReference;
private Button btnsend;
private EditText editText;
private ListView listView;
private ArrayList<String> arrayList = new ArrayList<>();
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatbox);
databaseReference = FirebaseDatabase.getInstance().getReference();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
btnsend = findViewById(R.id.sendbutton);
editText = findViewById(R.id.typemessage);
listView = findViewById(R.id.listmessages);
btnsend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
databaseReference.child("Chat").child("Name").push().setValue(editText.getText().toString());
}
});
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
DatabaseReference chatreference=databaseReference.child("Chat");
chatreference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
try {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String chatmsg = snapshot.child("Name").getKey();
Toast.makeText(Chatbox.this, "keys " + chatmsg, Toast.LENGTH_SHORT).show();
arrayList.add(chatmsg);
}
listView.setAdapter(adapter);
}
catch (Exception ex)
{
Toast.makeText(Chatbox.this, "Error " + ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Try this
FirebaseDatabase.getInstance()
.getReference("Chat")
.child("Name")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
List<String> chatMessages = new ArrayList<>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
try {
String chatMessage = String.valueOf(dataSnapshot.child(dataSnapshot1.getKey()).getValue());
chatMessages.add(chatMessage);
} catch (DatabaseException e) {
e.printStackTrace();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
callback.onFailure(null);
}
});
i guess you want to retrive data not the key:
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
DatabaseReference chatreference=databaseReference.child("Chat");
chatreference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String chatmsg = snapshot.child("Name").getValue();
Toast.makeText(Chatbox.this, "keys " + chatmsg, Toast.LENGTH_SHORT).show();
arrayList.add(chatmsg);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
or want you to store both in a array list key and value together?
You're adding the messages to the database with:
databaseReference.child("Chat").child("Name")
That means you must also read the messages from the same path:
databaseReference.child("Chat").child("Name").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String chatmsg = snapshot.getValue(String.class);
arrayList.add(chatmsg);
}
listView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException()l
}
});
The changes:
Listen to the same node as you're adding the messages to.
Removed the nested listener, which is not needed.
Get the value from each child snapshot, instead of trying to read a non-existing Name property.
Removed the try...catch as the default behavior is to write messages to logcat, which is more useful than showing them in a toast.
Implemented onCancelled, because you should never ignore errors.
I have been making a project using firebase for a delivery system app.
Problem: In this image, I need to get these two keys(or parent I think) and store them in a string or if possible I just want to know if I can check if the Id in user has a duplicate. How to check if these key has duplicate or exist and store it on a string if it does exist.
Updated code,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manageordergrocery);
mRecyclerView2= findViewById(R.id.recyclerview1);
mRecyclerView2.setHasFixedSize(true);
mRecyclerView2.setLayoutManager(new LinearLayoutManager(this));
mModel2 = new ArrayList<>();
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
mAdapter2=new ViewHolder1(Manageordergrocery.this, mModel2);
mRecyclerView2.setAdapter(mAdapter2);
mDatabaseReference2= FirebaseDatabase.getInstance().getReference("Users");
mDatabaseReference2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
String myParentNode = postSnapshot.getKey();
mDatabaseReference2 = FirebaseDatabase.getInstance().getReference();
mDatabaseReference2.orderByKey().equalTo(myParentNode).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String saveNode = dataSnapshot.getKey();
mDatabaseReference2 = FirebaseDatabase.getInstance().getReference(saveNode).child("Ordered");
mDatabaseReference2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren()) {
Model1 model2 = postSnapshot.getValue(Model1.class);
mModel2.add(model2);
}
mAdapter2.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Manageordergrocery.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
Can someone help me, please? I'm still new in firebase and don't know how to do it.
To obtain the two keys you can do the following:
mDatabaseReference2 = FirebaseDatabase.getInstance().getReference("User");
mDatabaseReference2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
String myParentNode = postSnapshot.getKey();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.orderByKey().equalTo(myParentNode).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot posts :dataSnapshot.getChildren())
{
String key = posts.getKey();
if(key.equals(myParentNode))
Toast.makeText(Manageordergrocery.this,"equals", Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Manageordergrocery.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
Here first you add a reference to node User then you loop inside that node and retrieve the key using getKey(). After that you attach another listener to the root location and using orderByKey().equalTo(myParentNode) you can check if you have two keys with the same value and do the required.
I have the following database and need some help retrieving the name and address from the user which is logged in currently.
I wrote the following code but the information is not showing in the textview
public class UserProfileActivity extends AppCompatActivity {
DatabaseReference userReference = FirebaseDatabase.getInstance().getReference();
TextView userName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
userName = (TextView) findViewById(R.id.userName);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
userName.setText(name);
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
};
}
}
This is what is currently happening when implementing the ValueEventListener
try this:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child(userid);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String address=dataSnapshot.child("address").getValue().toString();
String name=dataSnapshot.child("name").getValue().toString();
userName.setText(name);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Since your datasnapshot is on the child(userid) which is direct child under root node then you don't need to iterate to be able to get the values of the current user.