简体   繁体   English

Android 要求我将 final 声明为我想在 onclicklistner 函数中多次使用的值

[英]Android asks me to declare a final to a value I want to use more than once in a function onclicklistner

(Already checked the other question, this one is firebase related) Hello, so I am trying to repeatedly use a ref inside onClicklistner, but android studio asks me to declare a value as final, hence it only retrieves one item from the firebase.. This is my code which will show more details (已经检查了另一个问题,这是与 firebase 相关的)你好,所以我试图在 onClicklistner 中重复使用一个 ref,但是 android studio 要求我将一个值声明为 final,因此它只从 firebase 中检索一个项目..这是我的代码,它将显示更多详细信息

 DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Bathrooms");
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {



                for ( DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {


                    final Bathrooms bathrooms = dataSnapshot1.getValue(Bathrooms.class); // this is where I am asked and forced to create a final.

                    if( bathrooms.getAvailability().equals("Yes")) {


                        bathroomsArrayList.add(bathrooms);


                    }


                Browse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if(bathroomsArrayList!=null) {

                            if (bathrooms.getFloor().equals(Floor.getSelectedItem()) &&bathrooms.getSection().equals(Section.getSelectedItem())) {
                                    disable.setChecked(true);
                                    Toast.makeText(getActivity(), "Browse done successfully", Toast.LENGTH_SHORT).show();


                            }

                            else {
                                disable.setChecked(false);
                                Toast.makeText(getActivity(), "No bathrooms", Toast.LENGTH_SHORT).show();


                            }
                        }
                        else {
                            disable.setChecked(false);
                            Toast.makeText(getActivity(), "array empty", Toast.LENGTH_SHORT).show();


                        }

                    }
                });



                }

            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getActivity(), "Opsss.... Something is wrong", Toast.LENGTH_SHORT).show();

            }
        });



return rootview;

    }

guy.家伙。 the first way and actually the easiest one, I use in this situations, is to define your variable before onCreate(), then you can use it at every void you want.我在这种情况下使用的第一种方法也是最简单的方法是在 onCreate() 之前定义变量,然后你可以在你想要的每个空白处使用它。 think it helps you...觉得对你有帮助...

I cannot imagine why AndroidStudio would force you to final here.我无法想象为什么 AndroidStudio 会强迫你在这里完成。 Suggesting maybe.建议也许。 Anyways, since the variable is defined inside the for loop it will be created newly with each iteration anyways, so it is fine for it to be final -- ie it is initialized directly when it is created and is never assigned a new value.无论如何,由于变量是在 for 循环中定义的,它无论如何都会在每次迭代时新创建,所以它是最终的 - 即它在创建时直接初始化并且永远不会分配新值。

TL;DR: If your code is not working as expected, it is not the variable being final. TL;DR:如果您的代码没有按预期工作,则不是最终变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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