android饼图

pftdvrlh  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(250)

我正在使用一个选项卡式活动,在屏幕顶部显示一周中的几天。当我实现选项卡式活动时,饼图不再填充。如果删除选项卡式活动,饼图就会填充。我不知道这是什么原因请任何人帮忙。我将用注解掉的选项卡式活动代码粘贴代码。请帮忙

public class Breakdown extends Fragment {
    private final FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private final String userID = mAuth.getUid();
    private AnyChartView anyChartView;
    private final Date currentWeekDay = new Date();
    @SuppressLint("SimpleDateFormat")
    private final SimpleDateFormat simpleDateformat = new SimpleDateFormat("EEEE");
    private TextView calories, fats, proteins, carbohydrates;
    private double macroCalories, macroProteins, macroFats, macroCarbohydrates;
    private ViewPager viewPager;
    private FragmentActivity fragmentActivity;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_breakdown, container, false);
        anyChartView = view.findViewById(R.id.pieChart);
        calories = view.findViewById(R.id.dailyCaloriesLeftTextView);
        fats = view.findViewById(R.id.dailyFatsTV);
        proteins = view.findViewById(R.id.dailyProteinsTV);
        carbohydrates = view.findViewById(R.id.dailyCarbsTV);
        viewPager = view.findViewById(R.id.viewPager);
//        TabLayout tabLayout = view.findViewById(R.id.tabLayout);
//        tabLayout.addTab(tabLayout.newTab().setText("Mon"));
//        tabLayout.addTab(tabLayout.newTab().setText("Tue"));
//        tabLayout.addTab(tabLayout.newTab().setText("Wed"));
//        tabLayout.addTab(tabLayout.newTab().setText("Thurs"));
//        tabLayout.addTab(tabLayout.newTab().setText("Fri"));
//        tabLayout.addTab(tabLayout.newTab().setText("Sat"));
//        tabLayout.addTab(tabLayout.newTab().setText("Sun"));
//        MyAdapter adapter = new MyAdapter(getContext(), fragmentActivity.getSupportFragmentManager(), tabLayout.getTabCount());
//        viewPager.setAdapter(adapter);
//        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
//        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        getMacroObjectValues();
        calculateDailyMacros();
        setFields();
    }

//    @Override
//    public void onAttach(@NonNull Activity activity) {
//        fragmentActivity = (FragmentActivity) activity;
//        super.onAttach(activity);
//    }

    public void setUpPieChart(double fat, double protein, double carbs) {
        double[] macrosValues = {fat, protein, carbs};
        String[] macros = {"Fat", "Protein", "Carbs"};
        Pie pie = AnyChart.pie();
        List<DataEntry> dataEntries = new ArrayList<>();
        for (int i = 0; i < macros.length; i++) {
            dataEntries.add(new ValueDataEntry(macros[i], macrosValues[i]));
        }
        pie.data(dataEntries);
        anyChartView.setChart(pie);
        anyChartView.invalidate();
    }

    public void calculateDailyMacros() {
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("DayOfWeek").child(simpleDateformat.format(currentWeekDay));
        databaseReference.addValueEventListener(new ValueEventListener() {
            double calories, protein, carbohydrates, fats;
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot userSnapshot : snapshot.getChildren()) {
                    Meal meal = userSnapshot.getValue(Meal.class);
                    assert meal != null;
                    if (meal.getUserID().equals(userID)) {
                        protein = protein + meal.getItemProtein() * meal.getNumberOfServings();
                        fats = fats + meal.getItemTotalFat() * meal.getNumberOfServings();
                        carbohydrates = carbohydrates + meal.getItemTotalCarbohydrates() * meal.getNumberOfServings();
                        calories = calories + meal.getCalories() * meal.getNumberOfServings();
                        updateMacrosInDatabase(protein, fats, carbohydrates, calories);
                    }
                }
                setUpPieChart(fats, protein, carbohydrates);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(getContext(), "Error Occurred!" + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void getMacroObjectValues() {
        assert userID != null;
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Macros").child(userID);
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                Macro macro = snapshot.getValue(Macro.class);
                assert macro != null;
                macroCalories = macro.getCalorieConsumption();
                macroProteins = macro.getProteins();
                macroFats = macro.getFats();
                macroCarbohydrates = macro.getCarbohydrates();
            }

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

            }
        });
    }

    public void updateMacrosInDatabase(final double protein, final double fats, final double carbohydrates, final double calories) {
        assert userID != null;
        final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Macros").child(userID);
        databaseReference.child("proteins").setValue(macroProteins - protein);
        databaseReference.child("fats").setValue(macroFats - fats);
        databaseReference.child("carbohydrates").setValue(macroCarbohydrates - carbohydrates);
        databaseReference.child("calorieConsumption").setValue(macroCalories - calories);
    }

    public void setFields() {
        assert userID != null;
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Macros").child(userID);
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                Macro macro = snapshot.getValue(Macro.class);
                assert macro != null;
                calories.setText(String.valueOf(macro.getCalorieConsumption()));
                fats.setText(String.valueOf(macro.getFats()));
                proteins.setText(String.valueOf(macro.getProteins()));
                carbohydrates.setText(String.valueOf(macro.getCarbohydrates()));
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(getContext(), "Error Occurred!" + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题