How to call specific fields Firebase Database Android

2

Good morning, I have this model

 private Button verdosis, verhipo;
private String Lista="dosis";
private String lis2="Hipoglucemia";
private TextView textViewf,textViewhipo;
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseDatabase databas1 = FirebaseDatabase.getInstance();
DatabaseReference fReference = database.getReference().child(Lista);
DatabaseReference fReference2 = databas1.getReference().child(lis2);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);
    verdosis =(Button) findViewById(R.id.buttonverdosis);
    verhipo =(Button) findViewById(R.id.buttonverhipo);
    textViewf =(TextView) findViewById(R.id.textViewf);
    textViewhipo =(TextView) findViewById(R.id.textViewhipo);


    verdosis.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String valor = String.valueOf(dataSnapshot.getValue());
                    textViewf.setText(valor);
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });

            }

    });

and I want to be able to call show the data because it shows me this way and I do not want it is not easy to read I want you to show me after the first: points

Thanks

    
asked by Julian 26.09.2018 в 18:45
source

2 answers

1

You need to open a second loop to read the elements that will be in dosis and get their values.

You can save them in a StringBuilder and in the end get them in the TextView :

    final StringBuilder sb=new StringBuilder();

    fReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot oneDosis : dataSnapshot.getChildren()) {
                sb.append(oneDosis.getValue().toString());
                sb.append(System.getProperty("line.separator") );
            }
            textViewf.setText(sb.toString());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "onCancelled");
        }
    });

In a test I did with a structure similar to yours, I have this in the TextView :

dosis1
dosis2

If you take the dataSnapshot content on the screen, you will have something like this:

{ key = dosis, value = {1=dosis1, 2=dosis2} }

You therefore need a second loop to iterate over this part of the data and get its values:

{1=dosis1, 2=dosis2}

    
answered by 26.09.2018 / 20:27
source
0

I think I understand what you are trying to do I will explain it to you, so you can get Hypoglycemia, date and time separately, you must save them separately in the same child, that is to say you have the push son which is what puts you default firedatabase inside that child you put hypoglycemia and the value, date and value, time and value in this way you can extract it from a model class and it is easier for you, now you can create a split and leave it as you have it there would be something like that

String[] cadena = valor.split(" ");

and to claim the values or rather to obtain them you will have a vector such that in the first position cadena[0] you will have hypoglymia: 50 in the second position cadena[1] you will have date: 09/25/2018 and in the third position position cadena[2] you will have the time.

The same would happen for the case of doses.

I suggest you do it with a model class since it is much more orderly, as I try to explain it before, I hope I have helped you

    
answered by 26.09.2018 в 20:44