0

I have a Like Button in my RecyclerView Item. Value is fetched from the firebase database reference listener. If "count" value is 1, the button should get disabled for that particular listitem and also turn its color to green. Here is the code:

//segregate email to userID
    String[] emailSegregate = sharedPreferences.getString("email", "anonymous").split("@");
    String emailID = emailSegregate[0];

    //Check username has liked the post already or not
    mMessagesDatabaseReferenceCheck
            .child(emailID)
            .child(objectFile.getnodeId())
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    int b = 0;
                    check_counter objCheck = dataSnapshot.getValue(check_counter.class);
                    b = objCheck.getCount();
                    if (dataSnapshot.getChildrenCount() > 0) { // TODO: write this condition
                        //make it green
                        btnLike.setTypeface(null, Typeface.BOLD);
                        btnLike.setTextColor(Color.parseColor("#006400"));
                        btnLike.setEnabled(false);
                    }
                    else {}
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            });

Here is the Database:

enter image description here

Cant get one data from database and check the same on if Condition to disable a button for that particular nodeId listItem.

Edited

Here is the exception -

E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.abhin.myapplication, PID: 13674
  java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.abhin.myapplication.check_counter.getCounter()' on a null object reference
      at com.example.abhin.myapplication.postAdapter$2.onDataChange(postAdapter.java:148)
      at com.google.firebase.database.zzp.onDataChange(Unknown Source)
      at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
      at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
      at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5539)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

I think you're looking for something like this:

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    long count = dataSnapshot.child("count").getValue(Long.class);
    if (count > 0) {
        ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi Frank, No. Rather it throws this error - java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference – Abhinav Bhardwaj Aug 04 '18 at 23:00
  • That seems unlikely with the code I shared. Can you edit your question to include the complete stack trace of that exception? – Frank van Puffelen Aug 05 '18 at 02:34
  • Added the exception. – Abhinav Bhardwaj Aug 05 '18 at 08:56
  • As far as I can tell the code I shared does not raise that exception. What's on `postAdapter.java:148`? I'd also **highly** recommend studying [What is a `NullPointerException` and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it), as it will help you with this and similar problems in the future. – Frank van Puffelen Aug 05 '18 at 15:00
  • Sure Frank. Always ready to learn and get better. Will read this stuff. – Abhinav Bhardwaj Aug 05 '18 at 15:09