循环并分别返回每个项目

时间:2019-11-01 07:23:28

标签: java android loops for-loop mpandroidchart

我正在使用MpAndroidChart library,并尝试使用此方法设置自定义标签:

String[] daysOfTheWeek = {"M","T","W","T","F","S","S"};

XAxis xAxis = barChart.getXAxis();

xAxis.setValueFormatter(new ValueFormatter() { //Returns a string not a list :(
        @Override
        public String getFormattedValue(float value) {
            int position = 0;
            for(int i =0; i<daysOfTheWeek.length;i++){
                position = i;
            }
            return daysOfTheWeek[position];
        }
    });

但是我只得到数组的最后一项= S

如何单独退回所有物品?

5 个答案:

答案 0 :(得分:0)

使用此

String[] daysOfTheWeek = {"M","T","W","T","F","S","S"};

XAxis xAxis = barChart.getXAxis();

int position = 0;
for(int i =0; i<daysOfTheWeek.length;i++){
       position = i;
       xAxis.setValueFormatter(new ValueFormatter() { //Returns a string not a list :(
           @Override
           public String getFormattedValue(float value) {
                return daysOfTheWeek[position];

           }

        }
})

答案 1 :(得分:0)

抱歉,该方法接受float value。我只是不得不使用它!

xAxis.setValueFormatter(new ValueFormatter() {
    @Override
    public String getFormattedValue(float value) {
        return daysOfTheWeek[(int)value];
    }
});

答案 2 :(得分:0)

如果要返回数组的最后一项,请使用以下命令:

@Microsoft.KeyVault(SecretUri=Secret URI with version)

如果要单独获取商品,则可以创建一个获取商品位置并返回该商品的方法:

return daysOfTheWeek[daysOfTheWeek.length-1];

答案 3 :(得分:0)

尝试一下。

number_vector

答案 4 :(得分:0)

这个技巧对我来说100%可以显示从Mo到SU的工作日

chartWeeklyDistance.getXAxis().setAxisMinimum(0);
  
   String[] daysOfTheWeek = {"MO", "TU", "WE", "TH", "FR", "SA", "SU"};
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                if (((int) value) > -1 && ((int) value) < 7) {
                    return daysOfTheWeek[((int) value)];
                }
                return "";
            }
        });