java.lang.ArrayIndexOutOfBoundsException:rowIndex>可用行:3> 3

时间:2016-07-14 11:37:08

标签: android realm mpandroidchart

我在MpAndroidChart使用Realm制作图表时遇到问题,并且会抛出异常。我希望在XAxis上测量YAxis和体脂%的测量日期。我查看了Android设备监视器,数据保存正常,在id 1正确的字符串日期和浮动fatPercent下,在id 2 ...等等。更多信息如下。这是我的代码:

public class DataFatPercent extends RealmObject {


    private float id;
    private String date;
    private float fatPercent;

    public DataFatPercent() {

    }

    public DataFatPercent(float id, String date, float fatPercent) {
        this.id = id;
        this.date = date;
        this.fatPercent = fatPercent;
    }

使用setter和getter ......

我的活动:

public class FatGraphProgressActivity extends AppCompatActivity {

    ImageView dodaj_podatke_grafu;
    private LineChart lineChart;
    Realm realm;
    RealmResults<DataFatPercent> results;
    protected Typeface mTf;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fat_graph_progress);
        realm = Realm.getDefaultInstance();

        lineChart = (LineChart) findViewById(R.id.lineChart);
        setup(lineChart);


            lineChart.setExtraBottomOffset(5f);
            lineChart.getAxisLeft().setDrawGridLines(false);
            lineChart.getXAxis().setDrawGridLines(false);
            lineChart.getXAxis().setLabelCount(5);
            lineChart.getXAxis().setGranularity(1f);





        dodaj_podatke_grafu = (ImageView) findViewById(R.id.dodajpodatkevgraf);
        dodaj_podatke_grafu.setOnClickListener(new View.OnClickListener() {

            //Calling dialog,where I put data and save in realmbase
            @Override
            public void onClick(View v) {
                AddFatProcentDialog afpd = new AddFatProcentDialog();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                afpd.show(transaction,"dialog");
            }
        });
    }

    @Override
    protected void onResume(){
        super.onResume();

        setData();

    }

    public void setData(){

        results = realm.where(DataFatPercent.class).findAll();

        AxisValueFormatter formatter = new AxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return results.get((int) value).getDate();
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        };

        lineChart.getXAxis().setValueFormatter(formatter);

        RealmLineDataSet<DataFatPercent> lineDataSet = new RealmLineDataSet<>(results,"id","fatPercent");

        lineDataSet.setDrawCubic(false);
        lineDataSet.setLabel("Result Scores");
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setColor(ColorTemplate.rgb("#FF5722"));
        lineDataSet.setCircleColor(ColorTemplate.rgb("#FF5722"));
        lineDataSet.setLineWidth(1.8f);
        lineDataSet.setCircleSize(3.6f);

        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(lineDataSet);

        LineData lineData = new LineData(dataSets);
        styleData(lineData);

        // set data
        lineChart.setData(lineData);
        lineChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);

    }

对话框,当我点击图片来保存数据时:

public void save_data(){

        float id = 0;
        if(results.size()>0){
            id = results.max("id").floatValue();
        }
        newId = id + 1;
        String add_date = this.add_date.getText().toString();
        String percentoffat = add_fat_procent_data.getText().toString();
        float fat_percent_float = Float.parseFloat(percentoffat);

        realm.beginTransaction();
        dataFatPercent = new DataFatPercent(newId,add_date,fat_percent_float);
        podatkiOseb.getFatProcents().add(dataFatPercent);
        realm.commitTransaction();
        realm.close();

    }

    private View.OnClickListener click = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();

            switch (id){
                case R.id.potrdiprocentmascobe:
                    save_data();

            }
            dismiss();
        }
    };

}

简而言之,它会在getFormattedValue方法中抛出此错误:

  

引起:java.lang.ArrayIndexOutOfBoundsException:rowIndex&gt;   可用行:3&gt; 3                                                                                                     at io.realm.internal.TableView.nativeGetSourceRowIndex(Native Method)                                                                                                     at io.realm.internal.TableView.getSourceRowIndex(TableView.java:128)                                                                                                     在io.realm.RealmResults.get(RealmResults.java:205)at   com.example.draganmaric.tablayoutswipescroll.FatGraphProgressActivity $ 2.getFormattedValue(FatGraphProgressActivity.java:89)

如果我更改setData方法

,我也注意到了
 results = realm.where(DataFatPercent.class).findAll(); 

使用findallAsync,它不会抛出异常(它会打开活动),但我在屏幕上看不到任何图形。感谢您的任何建议

0 个答案:

没有答案