首页 > 解决方案 > 如何从 myRef.addValueEventListener 实例化一个全局变量

问题描述

使用myRef.addValueEventListener,我从 中获取一些数据firebase database,尝试将这些数据设置为全局变量,结果始终为 0。

public class EngineActivity extends AppCompatActivity implements EngineAdapter.OnDetailListener {

        private RecyclerView recyclerView;
        private RecyclerView.Adapter adapter;
        private List<ListItem> listItems;
        private TextView textViewRec;
        private double count;
        private double car_start;
        private double count1, check_engine_light;
        private double count2, coolant_leak;
        private double count3, engine_noise;
        private double count4, engine_oil;
        private double count5, engine_stall;
        private double count6, engine_temp;
        private double count7, idle_rough;
        private double count8, poor_fuel_economy;
        private double count9, smoke_exhaust;
        private double count10, vacuum_leak;
        private double count11, engine_overheating;
        private double sum;

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

            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("engine");
            myRef.addValueEventListener(new ValueEventListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    count = dataSnapshot.child("car_start").child("no_clicks").getValue(Double.class);
                    count1 = dataSnapshot.child("check_engine_light").child("no_clicks").getValue(Double.class);
                    Log.v("ankura", "Value is " + count1);
                    count2 = dataSnapshot.child("coolant_leak").child("no_clicks").getValue(Double.class);
                    count3 = dataSnapshot.child("engine_noise").child("no_clicks").getValue(Double.class);
                    count4 = dataSnapshot.child("engine_oil").child("no_clicks").getValue(Double.class);
                    count5 = dataSnapshot.child("engine_stall").child("no_clicks").getValue(Double.class);
                    count6 = dataSnapshot.child("engine_temp").child("no_clicks").getValue(Double.class);
                    count7 = dataSnapshot.child("idle_rough").child("no_clicks").getValue(Double.class);
                     count8 = dataSnapshot.child("poor_fuel_economy").child("no_clicks").getValue(Double.class);
                    count9 = dataSnapshot.child("smoke_exhaust").child("no_clicks").getValue(Double.class);
                    count10 = dataSnapshot.child("vacuum_leak").child("no_clicks").getValue(Double.class);
                    count11 = dataSnapshot.child("engine_overheating").child("no_clicks").getValue(Double.class);

                    sum = count + count1 + count2 + count3 + count4 + count5 + count6 + count7 + count8 + count9 + count10 + count11;
                    Log.v("ankura_sum", "Value is " + sum);


                    car_start = (count / sum) * 100;
                    check_engine_light = (count1 / sum) * 100;
                    coolant_leak = (count2 / sum) * 100;
                    engine_noise = (count3 / sum) * 100;

                    Log.v("MAINankura_engine_nois2", "Value is " + engine_noise);
                    engine_oil = (count4 / sum) * 100;
                    engine_stall = (count5 / sum) * 100;
                    engine_temp = (count6 / sum) * 100;
                    idle_rough = (count7 / sum) * 100;
                    poor_fuel_economy = (count8 / sum) * 100;
                    smoke_exhaust = (count9 / sum) * 100;
                    vacuum_leak = (count10 / sum) * 100;
                    engine_overheating = (count11 / sum) * 100;

                }

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

                }

            });


            listItems = new ArrayList<>();

            recyclerView = findViewById(R.id.recyclerView);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));

            listItems.add(new ListItem("Check Engine Light On", "Lorem Ipsum Lorem Ipsum", check_engine_light));
            listItems.add(new ListItem("Engine Oil Warning Light On", "Lorem Ipsum Lorem Ipsum", engine_oil));
            listItems.add(new ListItem("Engine Temperature Warning Light On", "Lorem Ipsum Lorem Ipsum", engine_temp));
            listItems.add(new ListItem("Engine Overheating", "Lorem Ipsum Lorem Ipsum", engine_overheating));
            listItems.add(new ListItem("Coolant Leak", "Lorem Ipsum Lorem Ipsum", coolant_leak));
            listItems.add(new ListItem("Car Won't start", "Lorem Ipsum Lorem Ipsum", car_start));

            listItems.add(new ListItem("Engine Idles Rough or Misfires", "Lorem Ipsum Lorem Ipsum", idle_rough));
            listItems.add(new ListItem("Engine Vacuum Leak", "Lorem Ipsum Lorem Ipsum", vacuum_leak));
            listItems.add(new ListItem("Engine Stalls", "Lorem Ipsum Lorem Ipsum", engine_stall));
            listItems.add(new ListItem("Poor Fuel Economy", "Lorem Ipsum Lorem Ipsum", poor_fuel_economy));
            listItems.add(new ListItem("Engine Noise", "Lorem Ipsum Lorem Ipsum", engine_noise));

            Log.v("ankura_engine_noise", "Value is " + engine_noise);
            listItems.add(new ListItem("Smoke In Exhaust", "Lorem Ipsum Lorem Ipsum", smoke_exhaust));

            adapter = new EngineAdapter(listItems, this, this);
            recyclerView.setAdapter(adapter);
        }


        public void setText(){

        }


        @Override
        public void OnDetailClick(int position) {
            Intent intent = new Intent(this, CheckEngLightActivity.class);
            intent.putExtra("selected_detail", listItems.get(position));
            startActivity(intent);
        }
    }

例如,我期望的值是car_start = 8.33330.0

标签: javaandroidfirebasefirebase-realtime-database

解决方案


推荐阅读