MPAndroidChart 饼图自定义图例显示不正确

MPAndroidChart Pie Chart Custom Legends are not displaying correctly

我已使用 MpAndroidChart 创建饼图并从中检索数据 firebase.I 需要显示自定义图例,但只显示一项,但饼图有超过 5items.Since getColor() 和 getLabels()已弃用,我使用 获取饼图的颜色和标签

listdataref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()) {
                Log.d("TAG", "IF else");
                /* new homePieTask().execute(dataSnapshot);*/

                ArrayList<PieEntry> entries = new ArrayList<>();

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    int total = ds.child("total").getValue(Integer.class);
                    String name = ds.child("expensesName").getValue(String.class);
                    entries.add(new PieEntry(total, name));


                }

                pieDataSet = new PieDataSet(entries,"Expense By Dates");
                pieData= new PieData(pieDataSet);
                homepiechart.setData(pieData);
                pieDataSet.setColors(color);
                Legend legend = homepiechart.getLegend();
                legend.setEnabled(false);
                int colorCodes[] = getColors(legend);
                String labels[] = getLabels(legend);
                for (int i = 0; i < getColors(legend).length -1; i++) {
                    LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams(
                            20, 20);
                    parms_legen_layout.setMargins(0, 0, 20, 0);
                    LinearLayout legend_layout = new LinearLayout(context);
                    legend_layout.setLayoutParams(parms_legen_layout);
                    legend_layout.setOrientation(LinearLayout.HORIZONTAL);
                    legend_layout.setBackgroundColor(colorCodes[i]);
                    relativeLayout.addView(legend_layout);

                    TextView txt_unit = new TextView(context);
                    txt_unit.setText(labels[i]);
                    relativeLayout.addView(txt_unit);
                }

最后我按照以下代码完成了这个,我不知道这是不是最好的方法,但它仍然很好用

 listdataref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                Log.d("Grid","On");
                List<String> labelsList=new ArrayList<>();
                if (getActivity() != null) {

                        Log.d("Grid", "OnOn");
                        for(int i=0;i< dataSnapshot.getChildrenCount();i++) {
                            for(DataSnapshot ds:dataSnapshot.getChildren()) {

                                String label = ds.child("expensesName").getValue(String.class);

                            labelsList.add(label);
                                List<Integer> intList = new ArrayList<Integer>();
                                for (int j : color)
                                {
                                    intList.add(j);
                                }


                                aClass = new LegendsClass(intList.get(i), labelsList.get(i));

                        }

                            legendsClasses.add(aClass);
                            gridViewAdapter = new GridViewAdapter(getActivity(), legendsClasses);
                            gridView.setAdapter(gridViewAdapter);
                            gridViewAdapter.notifyDataSetChanged();



                        Log.d("Grid", "After");
                    }


                }
                }else
            {
                Log.d("Grid","else");
                gridView.setVisibility(View.INVISIBLE);
            }

        }

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

        }
    });