无法从Android中的外部类访问内部类数据 [英] Not able to access inner class data from outer class in Android

查看:152
本文介绍了无法从Android中的外部类访问内部类数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.我无法访问signUp集合中的用户名.我正在获取价值,但无法从外部访问.

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 在此处输入图片描述

  @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 API是asynchronous,这意味着onDataChange()方法在调用后立即返回,并且它返回的Task中的回调将被调用一段时间之后.无法保证需要多长时间.因此,可能需要几百毫秒到几秒钟的时间才能获得该数据.由于该方法会立即返回,因此您尚未尝试从onDataChange()方法之外使用它的userName变量的值,也不会从回调中填充该值.

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 some time 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 it outside the onDataChange() method, will not have been populated from the callback yet.

基本上,您正在尝试从异步的API同步返回值.那不是一个好主意.您应该按预期异步处理API.

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.

此问题的快速解决方案是仅在onDataChange()方法内使用userName字符串,否则我建议您从此 视频 更好的理解.

A quick solve 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 anwser 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.

这篇关于无法从Android中的外部类访问内部类数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆