I am trying to use anychart to plot data stored from firebase database but I cannot retrieve data with ValueDataEntry.
I have already tested the connection to my database using System.out.println() to retrieve data and it's fine.
public class statistics extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statistics);
AnyChartView anyChartView = findViewById(R.id.any_chart_view);
anyChartView.setProgressBar(findViewById(R.id.progress_bar));
Cartesian cartesian = AnyChart.column();
final List<DataEntry> data = new ArrayList<>();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("sales");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()){
Sales sales = ds.getValue(Sales.class);
data.add(new ValueDataEntry(sales.getName(), sales.getSold()));
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Column column = cartesian.column(data);
column.tooltip()
.titleFormat("{%X}")
.position(Position.CENTER_BOTTOM)
.anchor(Anchor.CENTER_BOTTOM)
.offsetX(0d)
.offsetY(5d)
.format("${%Value}{groupsSeparator: }");
cartesian.animation(true);
cartesian.title("sample title");
cartesian.yScale().minimum(0d);
cartesian.yAxis(0).labels().format("${%Value}{groupsSeparator: }");
cartesian.tooltip().positionMode(TooltipPositionMode.POINT);
cartesian.interactivity().hoverMode(HoverMode.BY_X);
cartesian.xAxis(0).title("Product");
cartesian.yAxis(0).title("Revenue");
anyChartView.setChart(cartesian);
}
here is my class
public class Sales {
public String id;
public String name;
public int sold;
public Sales() {
}
public Sales(String id, String name, int sold) {
this.id = id;
this.name = name;
this.sold = sold;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSold() {
return sold;
}
public void setSold(int sold) {
this.sold = sold;
}
}
I just simply want to populate a bar graph with data from firebase.
Related
DataBase Structure
I am trying to get reference to Users from Chatlist. What i am trying to do is if a users is logged in he will see the users mentioned under is Uid in Chatlist node inside his recyclerview.
I am getting a the nullpointerexception in the following line
DatabaseReference UserRef = rootRef.child("Users").child(chatlist.getId());
Exception
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database##19.2.1:96)
at com.shivam.chatapp2.Fragments.Chats.onCreateView(Chats.java:112)
MyCode
public class Chats extends Fragment {
private RecyclerView recyclerView;
private UserAdapter userAdapter;
private List<User> mUsers;
FirebaseUser fuser;
DatabaseReference reference;
FloatingActionButton profile, credentials;
ValueEventListener ChatListListener;
List<Chatlist> usersList;
private ChatsFragmentAdapter chatsFragmentAdapter;
public Chats() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_chats, container, false);
profile = view.findViewById(R.id.fab_edit_profile_pic);
credentials = view.findViewById(R.id.fab_edit_name);
FabAction();
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
fuser = FirebaseAuth.getInstance().getCurrentUser();
usersList = new ArrayList<>();
reference = FirebaseDatabase.getInstance().getReference("Chatlist").child(fuser.getUid());
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
usersList.clear();
if(dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Chatlist chatlist = snapshot.getValue(Chatlist.class);
usersList.add(chatlist);
}
}
//chatList();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Chatlist chatlist=new Chatlist();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference UserRef = rootRef.child("Users").child(chatlist.getId());
UserRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
mUsers.add(user);
}
userAdapter = new UserAdapter(getContext(), mUsers, true);
recyclerView.setAdapter(userAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
return view;
}
Update
Chatlist.java
public class Chatlist {
public String id;
public String MessageCheck;
public Chatlist() {
}
public Chatlist(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
User.java
public class User {
private String id;
private String First;
private String EmailID;
private String imageURL;
private String status;
public User(String id, String first, String emailID, String imageURL,String status) {
this.id = id;
this.First = first;
this.EmailID = emailID;
this.imageURL = imageURL;
this.status=status;
}
public User() {
}
public User(String userid, String first_name, String eMail) {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirst() {
return First;
}
public void setFirst(String first) {
First = first;
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public String getEmailID() {
return EmailID;
}
public void setEmailID(String emailID) {
EmailID = emailID;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
start listening to the users node from your first query of chatlist node:
reference = FirebaseDatabase.getInstance().getReference("Chatlist").child(fuser.getUid());
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
usersList.clear();
if(dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Chatlist chatlist = snapshot.getValue(Chatlist.class);
usersList.add(chatlist);
//start listening to users node here
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference UserRef = rootRef.child("Users").child(chatlist.getId());
UserRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
.....
....
....
}
....
....
update
Make sure this line is removed:
Chatlist chatlist=new Chatlist();
update 2
don't do this inside your users query:
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
mUsers.add(user);
}
instead do this:
User user = dataSnapshot.getValue(User.class);
mUsers.add(user);
update 3
chatlist class:
public class Chatlist {
public String id;
public Chatlist(String id) {
this.id = id;
}
public Chatlist(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
users class:
public class User {
private String id;
private String First;
private String EmailId;
private String imageURL;
private String status;
public User(String id, String First, String EmailId, String imageURL,String status) {
this.id = id;
this.First = First;
this.EmailId = EmailId;
this.imageURL = imageURL;
this.status=status;
}
public User() {
}
public User(String userid, String first_name, String eMail) {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirst() {
return First;
}
public void setFirst(String first) {
this.First = first;
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public String getEmailID() {
return EmailId;
}
public void setEmailID(String emailID) {
this.EmailId = emailID;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
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);
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;
}
}
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