Hi i currently doing assignment and stuck at his issue, i was Trying to retrieve data from firebase using anychart with ValueDataEntry but get this error
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to int
Below Are the code for history.java and Expense.class.
public void setupPieChart() {
Pie pie = AnyChart.pie();
List<DataEntry> dataEntries = new ArrayList<>();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference("Expense");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot ds:snapshot.getChildren()){
Expense expense = ds.getValue(Expense.class);
dataEntries.add(new ValueDataEntry(expense.getItem(), expense.getAmount()));
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
pie.data(dataEntries);
anyChartView.setChart(pie);
}
public class Expense
public Expense() {
}
public Expense(String id, String item, int amount) {
this.id = id;
this.item = item;
this.amount = amount;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
CodePudding user response:
Try this -
I think the value for the amount is in String in Firebase, So you need to change the datatype as a String in the model class.
Then -
dataEntries.add(new ValueDataEntry(expense.getItem(),Integer.parseInt(expense.getAmount())));