简体   繁体   中英

Not able to access inner class data from outer class in Android

Here is my code. I am not able to access the username which is in signUp collection. I am getting the value but not accessible from outside.

So please tell me some method to do.

I am also pasting snapshot of my database there are two tables from which I need to access data and display then display in a recycler view. Need to display value in a list format.

I enter image description here

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


    recyclerView=(RecyclerView)findViewById(R.id.recycler);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    listItems=new ArrayList<>();
    mAuth = FirebaseAuth.getInstance();

    databaseReference= FirebaseDatabase.getInstance().getReference();
    databaseReference.child("post").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                post p=ds.getValue(post.class);
                String user=p.getUid();
              final  String nh,nd;
                nh=p.getNewsHeading();
                nd=p.getNewsDiscription();

                databaseReference= FirebaseDatabase.getInstance().getReference();
                databaseReference.child("signUp").child(user).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        signUp s=dataSnapshot.getValue(signUp.class);
                        Toast.makeText(MainActivity.this, s.getUser(), Toast.LENGTH_SHORT).show();
//want to access username from outside

                        userName =s.getUser();

                    }

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

                    }

                });

                p=new post(nh,nd,userName);
                listItems.add(p);
            }
            listItems.add(new post("hdfsgidsf","d,hdjh","ramu babu")) ;
            myAdapter adapter;
            adapter=new myAdapter(listItems,getApplicationContext());
            recyclerView.setAdapter(adapter);


        }

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

        }
    });



    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //******************************************************************************************************

}

Firebase APIs are asynchronous , meaning that onDataChange() method returns immediately after it's invoked, and the callback from the Task it returns will be called sometime later. There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because that method returns immediately, the value of your userName variable you're trying to use outside the onDataChange() method, will not have been populated from the callback yet.

Basically, you're trying to return a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.

A quick solution for this problem would be to use the userName string only inside the onDataChange() method, otherwise I recommend you see the last part of my answer from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Edit: Feb 26th, 2021

For more info, you can check the following article:

And the following video:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM