Home > Enterprise >  Android Studio Firebase MPChart
Android Studio Firebase MPChart

Time:11-11

I am a newbie in android studio. I want to know how many times "plastic" appears in the Firebase Document field. And show it in MPChart. I count 'Plastic' in Firebase Connect, but it can't work in data().How can I write Plastic?

public class MainChart {
BarChart barChart;
int Plastic;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chart);
    getMultipleDocs();
    BarData barData = new BarData();
    barData.addDataSet(barDataSet);
    barChart.setData(barData);
    barChart.invalidate();
}
public void getMultipleDocs() {
            .collection("Garbage")
            .whereEqualTo("name","plastic")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        List<DocumentSnapshot> snapshotList = queryDocumentSnapshots.getDocuments();
                        for(DocumentSnapshot snapshot:snapshotList){
                            Log.d(TAG, "onSuccess: " snapshot.getData().toString());
                        }
                        Plastic  ;
                        Log.d(getClass().getName(), "plastic have = "   Plastic);
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e(TAG, "onFailure: ", e);
                    }
                });
public ArrayList<BarEntry> data(){
    ArrayList<BarEntry> dataVal = new ArrayList<>();
    dataVal.add(new BarEntry(0,Plastic));
    return dataVal;
}

}

My getMultipleDocs() code

My data() code

CodePudding user response:

Data is loaded from Firestore (and most modern cloud APIs) asynchronously, since it may take some time. While the data is being loaded your main code continues to execute. Then when the data is available, your onSuccess gets called.

The result of this is that your dataVal.add(new BarEntry(0,Plastic)) runs before Plastic ever gets called. You can most easily validate that by running the code in a debugger and setting breakpoints, or with some well places log statements.

The solution is always the same: any code that needs the data from Firestore needs to be inside onSuccess or be called from there. So:

...
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            List<DocumentSnapshot> snapshotList = queryDocumentSnapshots.getDocuments();
            for(DocumentSnapshot snapshot:snapshotList){
                Log.d(TAG, "onSuccess: " snapshot.getData().toString());
            }
            Plastic  ;

            Log.d(getClass().getName(), "plastic have = "   Plastic);
            public ArrayList<BarEntry> data(){
                ArrayList<BarEntry> dataVal = new ArrayList<>();
                dataVal.add(new BarEntry(0,Plastic));
                return dataVal;
            }
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e(TAG, "onFailure: ", e);
        }
    });

For more on this see:

  • Related