如何在android中将局部变量复制到全局变量

时间:2018-02-28 23:23:38

标签: android

public class Main2Activity extends AppCompatActivity {

    String value; //how to make its value avliable in end of program
    //i want to use its value after ondatachange to picasso 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        // Write a message to the database
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("url");

        myRef.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String value = dataSnapshot.getValue(String.class);

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Toast.makeText(getApplicationContext(), "Hello JavatpointF", Toast.LENGTH_SHORT).show();
            }
        });
        Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();

        ImageView imageView = (ImageView) findViewById(R.id.image1);

        Picasso.with(this).load(value).into(imageView);
        //i did not getting the value here it showing blank toast

    }
}

1 个答案:

答案 0 :(得分:2)

替换String value = dataSnapshot.getValue(String.class); 截至value = dataSnapshot.getValue(String.class);