Search code examples
androidfirebasefirebase-realtime-databasetextview

How to read data off Textview if data is coming from a Firebase


Context:

The app is working fine and it works like this:

After the user is done taking a quiz he/she can go to the profile and see his/her score. like this:

Example:

Score: 10

But I want to something like this:

Score: 10
Result: You got perfect score

I put the code below. I don't really know what I am doing wrong, because I just get the data of the textview and then compare it to a number.

Code I am having problem in

//// PUT DATA IN RESULT MEANING ////

String result = getIntent().getExtras().get(String.valueOf(scoreTextview)).toString();

if (Integer.parseInt(result) == 10) {

    result_meaning.setText("Congrats");
}
else if ((Integer.parseInt(result) >= 5) && (Integer.parseInt(result) <= 9)) {

    result_meaning.setText("nc");
}
else
{
    result_meaning.setText("OK");
}
//// PUT DATA IN RESULT MEANING ////

Full code

ref2.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {

        Score userScore = snapshot.getValue(Score.class);

        if (userScore != null) {

            String score = userScore.result;

            scoreTextview.setText(score);

            //// PUT DATA IN RESULT MEANING ////

            String result = getIntent().getExtras().get(String.valueOf(scoreTextview)).toString();

            if (Integer.parseInt(result) == 10) {

                result_meaning.setText("Congrats");
            }
            else if ((Integer.parseInt(result) >= 5) && (Integer.parseInt(result) <= 9)) {

                result_meaning.setText("nc");
            }
            else {
                result_meaning.setText("OK");
            }
            //// PUT DATA IN RESULT MEANING ////
        }
    }

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

    }

});


Solution

  • If the score has a value then there isn't any need to get a score value from the textview again. You can do like this:

    if (Integer.parseInt(score) == 10) {
      result_meaning.setText("Congrats");
    }
    else if ((Integer.parseInt(score) >= 5) && (Integer.parseInt(score) <= 9)) {
       result_meaning.setText("nc");
    }
    else {
      result_meaning.setText("OK");
    }