Can anyone help me to retrieve data from every child except doctor_profile.
Do i need to create separate class for every child or there is another way to solve this problem.
TIA
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("addiction psychiatrist");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
Psychiatrist psychiatrist = snapshot.getValue(Psychiatrist.class);
//now you can add it to your list (collection) or do what you want
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
and of course you must have class which represents your objects, so for example:
#IgnoreExtraProperties
public class Psychiatrist {
private String name;
private String city;
private String hospital;
public Psychiatrist() {
// required empty constructor
}
public Psychiatrist(String name, String city, String hospital) {
this.name = name;
this.city = city;
this.hospital = hospital;
}
public String getName() {
return name;
}
public String getCity() {
return city;
}
public String getHospital() {
return hospital;
}
}
Related
I'm extracting a Boolean from Firebase. However, I'm getting an the Can't convert object of type java.lang.Boolean error when I pull this data.
How do I pull Boolean data?
List<Product> productList;
List<Boolean> productListStates;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
final DatabaseReference ref = mDatabase.child("0").child("states").child("001");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
Product p = snapshot.getValue(Product.class);
productListStates.add(p.getStates());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Product Class:
public class Product {
private String places;
private String time;
private String title;
private String id;
private boolean States;
public Product(){
}
public Product(String places, String time, String title, String id, boolean States) {
this.places = places;
this.time = time;
this.title = title;
this.id = id;
this.States = States;
}
public String getplaces() {
return places;
}
public String gettime() {
return time;
}
public String gettitle() {
return title;
}
public String getid() {
return id;
}
public boolean getStates() {
return States;
}
}
As I see in your schema, under the states node there is no object of type Product, is only a property of type boolean. That's why you get that error. To solve this, please use the following lines of code:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
final DatabaseReference ref = mDatabase.child("0").child("states").child("001");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
booblean b = snapshot.getValue(Boolean.class);
Log.d(TAG, String.valueOf(b));
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
The result in the logcat will be:
true
I want to draw boolean data from the database, but my code is wrong. How do I assign boolean data to the getstate variable? I want to assign the data in the database to the getstate variable in the product class. How do I do it?
I get this error on the line :
productList.add(p.getStates());
: I am getting the error:
list cannot be applied to boolean
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
final DatabaseReference ref = mDatabase.child("0").child("states");
List<Product> productList;
dbProducts.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
productList = new ArrayList<>();
if(dataSnapshot.exists()){
for(DataSnapshot productSnapshot : dataSnapshot.getChildren()){
Product p = productSnapshot.getValue(Product.class);
productList.add(p);
}
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
Product p = snapshot.getValue(Product.class);
productList.add(p.getStates());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
public class Product {
private String places;
private String time;
private String title;
private String id;
private boolean States;
public Product(){
}
public Product(String places, String time, String title, String id, boolean States) {
this.places = places;
this.time = time;
this.title = title;
this.id = id;
this.States = States;
}
public String getplaces() {
return places;
}
public String gettime() {
return time;
}
public String gettitle() {
return title;
}
public String getid() {
return id;
}
public boolean getStates() {
return States;
}
}
You are trying to add a Boolean value in a list that cotains a Product. The correct way to do it is:
productList.add(p);
I'm trying to use the listview using array adapter on my fragment but when trying to do it using the tutorial, It crashes every time i open that specific fragment. I'm trying to show the data on my firebase into the listview but it crashes every time i open it.
Here's my database
From the picture My plan is to only show the email and name of the user and not to include the type but I'll do that later.
And here's my code
public class memberFragment extends Fragment {
private ListView mylistView;
private DatabaseReference mDatabase;
ArrayList<String> myArrayList = new ArrayList<>();
FirebaseDatabase myFirebase;
public memberFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_member2, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users");
final ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,myArrayList);
mylistView = (ListView)view.findViewById(R.id.listView);
mylistView.setAdapter(myArrayAdapter);
mDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String myChildValues = dataSnapshot.getValue(String.class);
myArrayList.add(myChildValues);
myArrayAdapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
myArrayAdapter.notifyDataSetChanged();
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
// Inflate the layout for this fragment
return view;
Any ideas what might be causing the problem? I tried to searched and found out its related to hashmap or map, but I have no idea how to put it on my array?
The can use a model class in order to achieve this or you can use a simplest way, using the String class. For that, you can use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Accounts").child("Users");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String email = ds.child("email").getValue(String.class);
String name = ds.child("name").getValue(String.class);
Log.d("TAG", email + " / " + name);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
Your output will be:
a#b.com / idk
//and so on
If you want to get all values from reference data snapshot instead of dataSnapshot.getValue(String.class); you should create data class which contains these fields. As an example:
public class UserData {
private String name;
private String email;
private Integer type;
public UserData() {}
public UserData(String name, String email, Integer type) {
this.name = name;
this.email = email;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
}
Then using this class you can get all data of the user by using it like this:
UserData data = (UserData) dataSnapshot.getValue(UserData.class);.
And by using getters you can get the value you want.
The easier way would be to create an object for the list items you want to retrieve from Firebase and then use that object to set your list view items.
But if you are using simple list adapter then I suggest you make the following changes:
mDatabase.addValueEventListener(new ValueEventListener {
#Override
public void onDataChange(DataSnapshot dataSnapshot){
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String myChildValues = snapshot.getvalue(String.class);
myArrayList.add(myChildValues);
myArrayAdapter.notifyDataSetChange();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Do this accordingly for each of your string data under "Users".
This is my firebase database from where I want to retrieve all the names of universities in ListView click here to see image
myRef= FirebaseDatabase.getInstance().getReference();
myRef.child("Universities").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
database c = postSnapshot.getValue(database.class);
final String name = c.getuniName();
userNameList.add(name);
final ArrayAdapter<String> mutahirAdapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1 , userNameList);
mListView.setAdapter(mutahirAdapter);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Database Class
I added data directly in Firebase now I am not getting the data in the ListView i wanted to retrieve all the child's of universities in ListView
package com.mutahir.futureguide;
public class database {
String Name;
public String uniName(String Name) {
this.Name= Name;
return Name;
}
public String getuniName() {
return Name;
}
public void setuniName(String Name) {
this.Name = Name;
}
}
Hope it worked!
myRef= FirebaseDatabase.getInstance().getReference();
myRef.child("Universities").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
final String name = postSnapshot.getValue("name").toString();
userNameList.add(name);
}
if(!userNameList.isEmpty()){
final ArrayAdapter<String> mutahirAdapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1 , userNameList);
mListView.setAdapter(mutahirAdapter);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to Sort from top to bottom on Firebase Database. Can anyone help me? I tried to use collections, but it didn't work.
Activity where starts the recycler view
public class activity_ranking extends AppCompatActivity {
RecyclerView rv;
List<User> users;
Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranking);
rv = (RecyclerView)findViewById(R.id.recycler);
rv.setLayoutManager(new LinearLayoutManager(this));
users = new ArrayList<>();
FirebaseDatabase database = FirebaseDatabase.getInstance();
adapter = new Adapter(users);
Collections.sort(users, new Comparator<User>(){
public int compare(User obj1, User obj2)
{
// TODO Auto-generated method stub
return (obj1.getStars() > obj2.getStars()) ? +1: (obj1.getStars() > obj2.getStars()) ? 1:0 ;
}
});
rv.setAdapter(adapter);
Query query = database.getReference().child("users").orderByChild("stars");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
users.removeAll(users);
for (DataSnapshot snapshot:
dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
users.add(user);
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
MyClass user
public class User {
public User() {
}
String username;
String name;
int stars;
String id;
public User(String username, String name, int stars, String id) {
this.username = username;
this.name = name;
this.stars = stars;
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStars() {
return stars;
}
public void setStars(int stars) {
this.stars = stars;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
To invert the list, add items to the beginning instead of the end. So instead of:
users.add(user);
do
users.add(0, user);
See: Java Arrays how to add elements at the beginning