Store integer value into a variable from firebase

若如初见. 提交于 2020-06-27 05:54:32

问题


I have this little issue , I'm trying to get a number from my firebase database, the thing is that I can get the number but it seems that I'm doing something wrong storing it inside an integer.

What I'm trying to do is to have the number from firebase and compare it to my version number of my app, so I can tell my users to update the app if the apps is higher than my current version

here is my code

mDatabase.child("version").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
               // System.out.println(dataSnapshot.getValue());
                //Log.e("Versiondeapp",""+dataSnapshot.getValue());
                int ultimaVersion = (int) dataSnapshot.getValue();
                if(versionCode!=ultimaVersion){
                    ActualizarAppAlert();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

and my problem is here

int ultimaVersion = (int) dataSnapshot.getValue();

it seems that I can't store that number value from firebase to an integer variable

My current stack error

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference

PS: I solved my problem doing this Integer.valueOf(datasnapshot.getvalue().tostring());


回答1:


As I see that it's not exactly related to Firebase.
I think you need first to check why you object is null and than just convert it to int (that's what you need) with:

Integer.valueOf(dataSnapShot.getValue())

In addition take in consideration that according to Firebase documentation: The possible types returned from getValue() are:

Boolean  
Long  
Double  
Map<String, Object>  
List<Object>  



回答2:


If you are absolutely sure that the value is going to be an int number then this will work for you.

if(dataSnapshot.exists() && dataSnapshot.getValue()!= null){
    int ultimaVersion = ((Long) dataSnapshot.getValue()).intValue();
}



回答3:


kotlin

val result = dataSnapshot.value.toString()
desiredVariable = result.toInt()



回答4:


To solve this, please change this line of code:

int ultimaVersion = (int) dataSnapshot.getValue();

to:

int ultimaVersion = dataSnapshot.getValue(Integer.class);



回答5:


mDatabase.child("version").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                // System.out.println(dataSnapshot.getValue());
                //Log.e("Versiondeapp",""+dataSnapshot.getValue());
                int ultimaVersion =  ds.getValue(Integer.class);
                if(versionCode!=ultimaVersion){
                    ActualizarAppAlert();
            }

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Try this out..




回答6:


package rifqi.firebaseiotawal;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.concurrent.atomic.AtomicInteger;

import static android.R.id.message;


public class MainActivity extends AppCompatActivity {
private TextView txt;


   private FirebaseDatabase database = FirebaseDatabase.getInstance();
   private DatabaseReference myRef = database.getReference();
private  DatabaseReference mchild = myRef.child("adc");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 txt = (TextView) findViewById(R.id.text1);
txt.setText("hai");
    }
@Override
    protected void onStart () {
    super.onStart();
    mchild.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Integer adc = dataSnapshot.getValue(Integer.class);
                txt.setText(""+  adc);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
}

You can try this code, its work for me...im put "" at front of variable adc as type of data




回答7:


this works for me

int variableName=documentSnapshot.getLong("field name").intValue();



回答8:


Simply do

int ultimaVersion = Integer.parseInt(dataSnapshot.getValue()+"");

Here we have just appended an empty string to the Long value so as to make a whole string and convert it all to an integer using parseInt



来源:https://stackoverflow.com/questions/47423405/store-integer-value-into-a-variable-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!