get right data for specific user from Firebase - android

I have a chat app and I'm trying to pull down the chat groups that the logged in user is part of. I'm struggling to get down the right groups. Here is my firebase structure. All the members are under the "members" child inside a group. I'm displaying the groups in a recyclerview that works fine. This has of course work for any user that logs in and a user can be part of many different groups. I hope you get the idea and can help me. Thanks
My code:
private FirebaseDatabase mDatabase;
private DatabaseReference mDatabaseReference;
private FirebaseAuth mAuth;
private FirebaseUser mUser;
private String projectId;
private List<DataService> grouplist;
private RecyclerAdapter recyclerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mUser = mAuth.getCurrentUser();
mDatabaseReference = mDatabase.getReference().child("groups");
mDatabase = FirebaseDatabase.getInstance();
mDatabaseReference.keepSynced(true);
grouplist = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
protected void onStart() {
super.onStart();
grouplist.clear();
mDatabaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
DataService groupData = dataSnapshot.getValue(DataService.class);
grouplist.add(groupData);
recyclerAdapter = new RecyclerAdapter(Main.this, grouplist);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
DataService.java
public class DataService {
public String subject;
public String projectName;
public String endDate;
public String members;
public String userid;
public String creationDate;
public String dropbox;
public String evernote;
public String googleDocs;
public DataService() {
}
public DataService(String subject, String projectName, String endDate, String members, String userid, String creationDate, String dropbox, String evernote, String googleDocs) {
this.subject = subject;
this.projectName = projectName;
this.endDate = endDate;
this.members = members;
this.userid = userid;
this.creationDate = creationDate;
this.dropbox = dropbox;
this.evernote = evernote;
this.googleDocs = googleDocs;
}
public String getCreationDate() {
return creationDate;
}
public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}
public String getDropbox() {
return dropbox;
}
public void setDropbox(String dropbox) {
this.dropbox = dropbox;
}
public String getEvernote() {
return evernote;
}
public void setEvernote(String evernote) {
this.evernote = evernote;
}
public String getGoogleDocs() {
return googleDocs;
}
public void setGoogleDocs(String googleDocs) {
this.googleDocs = googleDocs;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getmembers() {
return members;
}
public void setmembers(String members) {
this.members = members;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
}

Using the actual database structure, you cannot get all the groups a user is in, unless you make some changes to your database. In your case, you should consider augmenting your database structure to allow a reverse lookup by creating another node, named groups under each user object, in which you should add all the group ids from which the user is appart of. So your database structure should look like this:
Firebase-root
|
--- users
|
--- userId
|
--- groups
|
--- groupIdOne: true
|
--- groupIdTwo: true
To get all the groups a user is appart, just attach a listener on the groups node and there are all the groups that you need.
But you'll be thinking, why to do this? Why to duplicate data? Well, there is no problem with duplicating data, when it comes to Firebase. This is a quite common practice, which is named denormalization and for that, I recommend you see this video, Denormalization is normal with the Firebase Database.
When you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.

Related

How to increment value in Firebase database android

Hopefully someone can help me with this. Lets say I have a post and I want to track down the number of clicks it gets. How do I go about incrementing the value in the database (Firebase). For example the post has one click that count as 1 then it has another click that counts as 2 then so on and so fourth. I tried doing something but I'm getting this error. I really need your help on this. Thanks in advance
Error message
Can't convert object of type java.lang.Long to type com.myapp.poopy.Model_Information
My code
databaseReference = FirebaseDatabase.getInstance("https://poopy-43981.firebaseio.com/").getReference().child("Post").child(getid);
int count;
count = count ++;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
databaseReference.child("clicks").setValue(count+1);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
});
Json
{
"Post" : {
"lG71ennq86UIZ5a5Oqh8oaYAXe03" : {
"-MHNWmVK3rBnvnr0qcph" : {
"clicks" : 0,
"id" : "-MHNWmVK3rBnvnr0qcph",
}
}
},
Model_class
public Model_Information(String category, String headline, String mImageUrl,String created,String time,String id,String status ) {
this.mImageUrl = mImageUrl;
this.category = category;
this.headline = headline;
this.created=created;
this.time=time;
this.id=id;
this.status=status;
}
public Model_Information() {
}
public String getstatus() {
return status;
}
public void setstatus(String status) {
status = status;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getHeadline() {
return headline;
}
public void setHeadline(String headline) {
this.headline = headline;
}
//Class using model class
databaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String previousChildName) {
if (dataSnapshot.exists()) {
progressBar.setVisibility(View.GONE);
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
myUploads.clear();
Model_Information upload = postsnapshot.getValue(Model_Information.class);
myUploads.add(upload);
aAdapter = new Adapter(MainActivity2.this, myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
recyclerView.invalidate();
}
}
}
To be able to update the clicks value you need to know the complete path to that value. In your use-case there seem to be two variables in that path:
the UID
a push ID
Once you know both values, increment the value can be done with the (relatively new) ServerValue.increment operation like this:
DatabaseReference rootReference = FirebaseDatabase.getInstance("https://poopy-43981.firebaseio.com/");
DatabaseReference userReference = rootReference.child("Post").child(getid);
DatabaseReference countReference = userReference.child("-MHNWmVK3rBnvnr0qcph").child("clicks");
countReference.setValue(ServerValue.increment(1));

How do I store a data into the firebase database without updating it? and also retrieving the data

I am currently working on a project that might be useful in a store for the ordering of foods. The device can already store some data and retrieve but there are problems that I have been dealing with.
Problem 1#: First off is that every time I store a data it usually looks like this:
For some reason I tried to use child "02" because it displays in the recycler view if I do something like "Ordering" as a child it does not seem to be showing in the display. How do I add more data to it like example in the child 02 I can still add like milkshakes or candy bars? This is the code I have done for storing.
public class Detailsoforder extends AppCompatActivity {
private static final String TAG = "AddToDatabase";
private TextView titles;
private TextView increase;
private int count = 0;
//add Firebase
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailsoforder);
titles = findViewById(R.id.Order);
increase = findViewById(R.id.Increase);
String title = getIntent().getStringExtra("title");
titles.setText(title);
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Object value = dataSnapshot.getValue();
Log.d(TAG,"Value is"+value);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
public void onIncreaseClick(View view) {
count++;
increase.setText(String.valueOf(count));
}
public void onOrderNow(View view) {
String value = increase.getText().toString();
if (value.equals("1")) {
Toast.makeText(Detailsoforder.this,"The order must be above 1", Toast.LENGTH_LONG).show();
}
else {
Log.d(TAG, "onClick: Attempting to add object to database.");
String newFood = titles.getText().toString();
if (!newFood.equals("")) {
FirebaseUser user = mAuth.getCurrentUser();
String userID = user.getUid();
myRef.child(userID).child("02").child("food").setValue(newFood);
myRef.child(userID).child("02").child("order").setValue(value);
Toast.makeText(Detailsoforder.this,"Adding " + newFood + " to database...", Toast.LENGTH_LONG).show();
//reset the text
titles.setText("");
Intent intent = new Intent(Detailsoforder.this, Placeorder.class);
startActivity(intent);
}
}
}
#Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
#Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
}
How do I store many data and not needing for updating it?
Problem 2#: Retrieving of the data. The problem is that I can only seem to get only 1 data. I wanted to fix the store part first so that I could check if I could get the many information. This is my code for retrieving of the data.
public class Vieworders extends AppCompatActivity {
private RecyclerView mRecyclerView1;
private ViewHolder1 mAdapter1;
private DatabaseReference mDatabaseReference1;
private List<Model1> mModel1;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vieworders);
mRecyclerView1= findViewById(R.id.recyclerview1);
mRecyclerView1.setHasFixedSize(true);
mRecyclerView1.setLayoutManager(new LinearLayoutManager(this));
mModel1 = new ArrayList<>();
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
String userID = user.getUid();
mAdapter1=new ViewHolder1(Vieworders.this, mModel1);
mRecyclerView1.setAdapter(mAdapter1);
mDatabaseReference1= FirebaseDatabase.getInstance().getReference(userID);
mDatabaseReference1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
Model1 model1=postSnapshot.getValue(Model1.class);
mModel1.add(model1);
}
mAdapter1.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Vieworders.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
Can someone help me, please? I really need this to be done.
I think you are able to send data to fire and able to store these datas.
Now ,this is my snipshots of fetching data from Firebase .You can take help from this code.
private RecyclerView recyclerView;
private List<RoomRentData> firebaselist;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
private DualProgressView progressView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_rent_activity);
firebaselist=new ArrayList<>();
recyclerView=findViewById(R.id.rent_item);
progressView=findViewById(R.id.progressbar);
progressView.setVisibility(View.VISIBLE);
recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),1));
recyclerView.setItemAnimator( new DefaultItemAnimator());
recyclerView.hasFixedSize();
final Calendar today = Calendar.getInstance();
String year=Integer.toString(today.get(Calendar.YEAR));
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference("room_rent").child(year);
mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
progressView.setVisibility(View.GONE);
System.out.println("1");
RoomRentData user = dataSnapshot.getValue(RoomRentData.class);
if (user == null) {
System.out.println("2");
Toast.makeText(ShowRentActivity.this,"No data found",Toast.LENGTH_LONG).show();
return;
}else {
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
System.out.println("datasnapshot 1"+dataSnapshot1);
System.out.println("3");
RoomRentData userdetails = dataSnapshot1.getValue(RoomRentData.class);
System.out.println("userdetails 1"+userdetails);
RoomRentData listdata = new RoomRentData();
String month=userdetails.getMonth();
String year=userdetails.getYear();
String cuRead=userdetails.getCurrentRead();
String prevRead=userdetails.getPrevUnit();
String totalRent=userdetails.getTotalRoomRent();
String perPersonCost=userdetails.getPerpersonCost();
String totalPeron=userdetails.getTotalPerson();
String paidOn=userdetails.getCurrentTimeAndDate();
String description=userdetails.getDescription();
System.out.println("description 1"+description);
System.out.println("month"+month);
listdata.setMonth(month);
listdata.setYear(year);
listdata.setCurrentRead(cuRead);
listdata.setPrevUnit(prevRead);
listdata.setTotalRoomRent(totalRent);
listdata.setPerpersonCost(perPersonCost);
listdata.setTotalPerson(totalPeron);
listdata.setCurrentTimeAndDate(paidOn);
listdata.setDescription(description);
firebaselist.add(listdata);
}
rentAdapter firebaseListAdapter=new rentAdapter(getApplicationContext(),firebaselist);
recyclerView.setAdapter(firebaseListAdapter);
}
}
#Override
public void onCancelled(DatabaseError error) {
progressView.setVisibility(View.GONE);
System.out.println(error);
System.out.println("error");
}
});
}
}
I have create a Model Class named RoomRentData.class.I have added this data to the RecyclerView using this model.
And This is my Model Class Code.
public class RoomRentData {
private String month;
private String year;
private String roomRent;
private String perUnitCost;
private String PrevUnit;
private String CurrentRead;
private String totalUnitCostt;
private String totalRoomRent;
private String totalPerson;
private String perpersonCost;
private String description;
private String currentTimeAndDate;
public String getCurrentRead() {
return CurrentRead;
}
public String getCurrentTimeAndDate() {
return currentTimeAndDate;
}
public String getDescription() {
return description;
}
public String getMonth() {
return month;
}
public String getPerpersonCost() {
return perpersonCost;
}
public String getPerUnitCost() {
return perUnitCost;
}
public String getPrevUnit() {
return PrevUnit;
}
public String getRoomRent() {
return roomRent;
}
public String getTotalPerson() {
return totalPerson;
}
public String getTotalRoomRent() {
return totalRoomRent;
}
public String getTotalUnitCostt() {
return totalUnitCostt;
}
public String getYear() {
return year;
}
public void setCurrentRead(String currentRead) {
CurrentRead = currentRead;
}
public void setCurrentTimeAndDate(String currentTimeAndDate) {
this.currentTimeAndDate = currentTimeAndDate;
}
public void setDescription(String description) {
this.description = description;
}
public void setMonth(String month) {
this.month = month;
}
public void setPerpersonCost(String perpersonCost) {
this.perpersonCost = perpersonCost;
}
public void setPerUnitCost(String perUnitCost) {
this.perUnitCost = perUnitCost;
}
public void setPrevUnit(String prevUnit) {
PrevUnit = prevUnit;
}
public void setRoomRent(String roomRent) {
this.roomRent = roomRent;
}
public void setTotalPerson(String totalPerson) {
this.totalPerson = totalPerson;
}
public void setTotalRoomRent(String totalRoomRent) {
this.totalRoomRent = totalRoomRent;
}
public void setTotalUnitCostt(String totalUnitCostt) {
this.totalUnitCostt = totalUnitCostt;
}
public void setYear(String year) {
this.year = year;
}
}

Show list of data on my firebase database

Hello I'm trying to show the data's that i have inputted in my activity, but the problem is i don't have any idea how to, I'm at lost on the data snapshot one. I'm trying to like only show the name and email
My database
my code and progress so far
public class AdminActivity extends AppCompatActivity {
private DrawerLayout mdrawerl;
private ActionBarDrawerToggle mtoggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
collectName((Map<String,Object>)dataSnapshot.getValue());
}
private void collectName(Map<String, Object> value) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
reference that i used How to get all child's data in firebase database?
1- Create a model class that will hold the objects of user. In your case it would be
public class User {
private String email;
private String name;
private int type;
public User() {
}
public User(String email, String name, int type) {
this.email= email;
this.name= name;
this.type= type;
}
}
2 - Create a ArrayList that will hold items of User
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<User> userList = new ArrayList();
for (DataSnapshot child :
dataSnapshot.getChildren()) {
User user = child.getValue(User.class);
userList.add(user )
}
//Here you have all user you can pass ```userList``` to your method furthur
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Hope this will help you.

Fetch data from firebase in Android

Not able to fetch the data it gives "Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()".Not Able to find any solution please help.
The Code is
public class AdminUser extends AppCompatActivity {
private TextView tv1,tv2;
private DatabaseReference mDatabase;
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_user);
auth = FirebaseAuth.getInstance();
String username=FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabase= FirebaseDatabase.getInstance().getReference().child(username);
tv1=(TextView)findViewById(R.id.textView6);
tv2=(TextView)findViewById(R.id.textView8);
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// String name=dataSnapshot.getValue().toString();
String name=dataSnapshot.child("name").getValue(String.class).toString() ;
String address=dataSnapshot.child("address").getValue(String.class).toString();
tv1.setText(name);
tv2.setText(address);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
This 2 lines:
String name=dataSnapshot.child("name").getValue(String.class).toString() ;
String address=dataSnapshot.child("address").getValue(String.class).toString();
are causing your problem, go to the Firebase documentation and read about how to read data from the realtime database here
https://firebase.google.com/docs/database/android/read-and-write
Edit
To give you an example specific to your question, first you need to create a Plain Old Java Object (POJO) class that represent the data you have in your database, for example.
public class User {
private String name;
private String address;
public User() {
//Empty constructor required by firebase
// serialize the data retrieved and convert
// it to an object of this class
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Then in your valueEventListener you can do something like this
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Serialize your retrieved data to a User object
User user = dataSnapshot.getValue(User.class);
//Now you have an object of the User class and can use its getters like this
tv1.setText(user.getName());
tv2.setText(user.getAddress());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Hope this helps

using Map to get object of custom class

I'm new at using maps, how can we get object of custom Class from Map..
i.e.
feedSendModelClass:
public class feedSendModelClass {
private String content;
private String picThumbnail;
private String picurl;
private String videourl;
private String videoThumbnail;
private String mTime;
public feedSendModelClass() {
//blank for firebase
}
public String getTime() {
return mTime;
}
public void setTime(String time) {
mTime = time;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getVideoThumbnail() {
return videoThumbnail;
}
public void setVideoThumbnail(String videoThumbnail) {
this.videoThumbnail = videoThumbnail;
}
public String getVideourl() {
return videourl;
}
public void setVideourl(String videourl) {
this.videourl = videourl;
}
public String getPicurl() {
return picurl;
}
public void setPicurl(String picurl) {
this.picurl = picurl;
}
public String getPicThumbnail() {
return picThumbnail;
}
public void setPicThumbnail(String picThumbnail) {
this.picThumbnail = picThumbnail;
}
}
Map
Map<String, feedSendModelClass> feedKeys = (Map<String,feedSendModelClass>) dataSnapshot.getValue();
Datasnapshot:
DataSnapshot { key = feedsMetaPool, value = {20160708155218759={content=bzzzhss, picurl=https://firebasestorage.googleapis.com/v0/b/rootssahaj.appspot.com/o/feedsImages%2FnonThumbs%2F201607081552353353553518759sahaj9917730102?alt=media&token=ce1c6e7c-28c5-442e-9405-4468bf5fc204, time=08 Jul, picThumbnail=https://firebasestorage.googleapis.com/v0/b/rootssahaj.appspot.com/o/feedsImages%2Fthumbs%2F20353353535355353160708155218759sahaj9917730102?alt=media&token=14d33963-f3da-4407-a08f-ab0afce21ace}, 20160708164100152={content=sj sjs skss sksssvsjm, picurl=https://firebasestorage.googleapis.com/v0/b/rootssahaj.appspot.com/o/feedsImages%2FnonThumbs%2F201607081641003535152sahaj9917730102?alt=media&token=323c4a27-1850-4bc9-adea-34e5319ac506, time=08 Jul, picThumbnail=https://firebasestorage.googleapis.com/v0/b/rootssahaj.appspot.com/o/feedsImages%2Fthumbs%2F20160708164100152sahaj9917730102?alt=media&token=0b064b46-2f14-49af-b3e6-7a573f6c2d3f}, 20160708142314069={content=dadax, picurl=https://firebasestorage.googleapis.com/v0/b/rootssahaj.appspot.com/o/feedsImages%2FnonThumbs%2F2016070rdgdg54353535358142314069sahaj9917730102?alt=media&token=19d03f11-d12f-43c1-96f5-ca397a9179c9, time=08 Jul, picThumbnail=https://firebasestorage.googleapis.com/v0/b/rootssahaj.appspot.com/o/feedsImages%2Fthumbs%2F20160735353508142314069sahaj9917730102?alt=media&token=ab9c73eb-ec44-46ae-9daa-6fc508cb08a6} }
This has become tricky for me..Thanks..
There could be some other way round to do it through firebase
If you are asking how you retrieve the feedSendModelClass object from the map named feedKeys, then this syntax should work:
feedKeys.get(key)
The key is your String, and the object is the feedSendModelClass object. However, first you'd need to put key and value pairs into the map (so it's not empty), with feedKeys.put(key, value), where the key is a String and value is the feedSendModelClass.
If this totally doesn't answer your question, I apologize, but from your description, it is a bit difficult to understand what you need help with.
On an unrelated note, it would be more appropriate to name your class FeedSendModelClass, as per Java conventions classes start with a capital letter.
If you're using Firebase then you can use a ChildEventListener. It calls the onChildAdded() method once for every initial child in the node and everytime a new child is added. Then you can add the Object to an ArrayList directly (or do whatever you want with it)
ArrayList<feedSendModelClass> feedKeysArrayList = new ArrayList<>();
DatabaseReference feedKeysRef = FirebaseDatabase.getInstance().getReference("path/to/the/feedkeys");
feedKeysRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
feedKeysArrayList.add(dataSnapshot.getValue(feedSendModelClass.class));
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
// Add appropriate code
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
// Add appropriate code
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
// Add appropriate code
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Add appropriate code
}
});

Categories

Resources