2

I'm using GraphView library. My code below gets data from Firebase real-time database and updates the graph with values from database, but it isn't sorted. I need it to be sorted in ascending order in X axis. The data in X axis is Long type, then it's converted to date.

I've tried adding dp[index] to array list and using different sorting methods, but nothing seems to work.

@Override
public void onStart() {
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            FirebaseAuth auth;
            auth = FirebaseAuth.getInstance();
            FirebaseUser user = auth.getCurrentUser();
            String id = user.getUid();
            DataPoint[] dp = new DataPoint[(int) snapshot.child(id).getChildrenCount()];
            int index = 0;
            for(DataSnapshot myDataSnapshot : snapshot.child(id).getChildren()){
                PointValue pointValue = myDataSnapshot.getValue(PointValue.class);
                dp[index] = new DataPoint(pointValue.getxValue(), pointValue.getyValue());
                index ++;
            }

            lineGraphSeries.resetData(dp);


        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
    super.onStart();
}

AlertDialog, where data is being written to the database:

builder.setPositiveButton("ADD",
  new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
            if(TextUtils.isEmpty(weightEditText.getText().toString()) || TextUtils
                    .isEmpty(dateEditText.getText().toString())) {

                Toast.makeText(getActivity(),"Please provide weight and date",
                        Toast.LENGTH_SHORT).show();

            } else{
                linearLayout.addView(createNewTextView(weightEditText.getText().toString(),
                        dateEditText.getText().toString()));

                String id = user.getUid();
                String randomId = reference.push().getKey();

                try {
                    Date date = sdf.parse(dateEditText.getText().toString());
                    long x = date.getTime();
                    int y = Integer.parseInt(weightEditText.getText().toString());
                    PointValue pointValue = new PointValue(x,y);
                    reference.child(id).child(randomId).setValue(pointValue);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

    }

});
builder.setView(view);

builder.show();

PointValue class:

public class PointValue {
    long xValue;
    int yValue;

    public PointValue() {
    }

    public PointValue(long xValue, int yValue) {
        this.xValue = xValue;
        this.yValue = yValue;
    }

    public long getxValue() {
        return xValue;
    }

    public int getyValue() {
        return yValue;
    }
}

@EDIT

Thanks to @Frank van Puffelen, I've managed to sort my data from Firebase Database using orderByChild() https://firebase.google.com/docs/database/android/lists-of-data#sort_data

below working solution:


@Override
    public void onStart() {
        FirebaseAuth auth;
        auth = FirebaseAuth.getInstance();
        FirebaseUser user = auth.getCurrentUser();
        String id = user.getUid();

        Query dateAscendingOrder = database.getReference("users")
                .child(id).orderByChild("xValue");

        dateAscendingOrder.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                DataPoint[] dp = new DataPoint[(int) snapshot.getChildrenCount()];
                int index = 0;
                for (DataSnapshot myDataSnapshot : snapshot.getChildren()){

                    PointValue pointValue = myDataSnapshot.getValue(PointValue.class);
                    dp[index] = new DataPoint(pointValue.getxValue(), pointValue.getyValue());
                    index++;
                }


                lineGraphSeries.resetData(dp);
            }

Cuyer
  • 95
  • 8
  • 1
    Did you see https://firebase.google.com/docs/database/android/lists-of-data#sort_data? `orderByChild()` sounds like it might be what you need. – Frank van Puffelen May 05 '22 at 23:57
  • Yes I did, but unfortunately I couldn't make it work. I'll try to use it again if it might be the solution. Thank You. – Cuyer May 06 '22 at 00:05
  • If you tried `orderByChild()` and couldn't get it to work, edit your question to show what you tried with that, and include an example of the data at `reference` (as text please, no screenshots). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen May 06 '22 at 03:39
  • Please edit your question and add the information Frank asked for, and please also respond with @. – Alex Mamo May 06 '22 at 07:34

0 Answers0