1

我的任务是从数据库中获取值并显示在图表上。

所以我的代码是这样的:

   private void displaygraph(){
    // TODO Auto-generated method stub
             try{
                 database=openOrCreateDatabase("medicaldb", MODE_PRIVATE, null);
     cursor3=database.query("patienttestdetails", new String[]{"date","sugarlevel"}, "pid="+patientId+"", null, null, null, "ptdid desc", "5"); 
     if(cursor3.getCount()==0)
            Toast.makeText(BloodsugarActivity.this, "Add Your Bloodsugar Details To See Here", Toast.LENGTH_LONG).show();

            while(cursor3.moveToNext())

                dates+=cursor3.getString(cursor3.getColumnIndex("date"))+",";


            }

            catch(SQLException exception)
            {}
            cursor3.close();


         // TODO Auto-generated method stub
             try{
                 database=openOrCreateDatabase("medicaldb", MODE_PRIVATE, null);
     cursor4=database.query("patienttestdetails", new String[]{"date","sugarlevel"}, "pid="+patientId+"", null, null, null, "ptdid desc", "5"); 
     if(cursor4.getCount()==0)
            Toast.makeText(BloodsugarActivity.this, "Add Your Bloodsugar Details To See Here", Toast.LENGTH_LONG).show();

            while(cursor4.moveToNext())


                sugarlevels+=cursor4.getString(cursor4.getColumnIndex("sugarlevel"))+",";

            }

        catch(SQLException exception)
        {}
           cursor4.close();     

            datearrays = dates.split(",");
            sugarlevelarrays=sugarlevels.split(",");

             // sugarlevelarrays = sugarlevels.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
              Number[] resultss = new Number[sugarlevelarrays.length];

              for (int i = 0; i < sugarlevelarrays.length; i++) {
                  try {
                      resultss[i] = Integer.parseInt(sugarlevelarrays[i]);
                  } catch (NumberFormatException nfe) {};
              }


              xyPlot = (XYPlot) findViewById(R.id.xyplot);

              // Converting the above income array into XYSeries
                XYSeries incomeSeries = new SimpleXYSeries(
                        Arrays.asList(resultss),                 // array => list
                        SimpleXYSeries.ArrayFormat.Y_VALS_ONLY , // Y_VALS_ONLY means use the element index as the x value
                        "SugarLevel");                               // Title of this series

                // Create a formatter to format Line and Point of income series
                LineAndPointFormatter incomeFormat = new LineAndPointFormatter(
                        Color.rgb(0, 0, 250),                   // line color
                        Color.rgb(200, 200, 200),               // point color
                        null );                                 // fill color (none)


                // Create a formatter to format Line and Point of expense series
                LineAndPointFormatter expenseFormat = new LineAndPointFormatter(
                        Color.rgb(255, 0, 0),                   // line color
                        Color.rgb(0, 200, 200),               // point color
                        null);                                  // fill color (none)



                // add income series to the xyplot:
                xyPlot.addSeries(incomeSeries, incomeFormat);


                // Formatting the Domain Values ( X-Axis )
                xyPlot.setDomainValueFormat(new Format() {

                    @Override
                    public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
                        return new StringBuffer( datearrays[ ( (Number)obj).intValue() ]  );                
                    }

                    @Override
                    public Object parseObject(String source, ParsePosition pos) {
                        return null; 
                    }
                });        

                xyPlot.setDomainLabel("");
                xyPlot.setRangeLabel("BloodSugar Graph");

                // Increment X-Axis by 1 value
                xyPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1);

                xyPlot.getGraphWidget().setRangeLabelWidth(50);               

                // Reduce the number of range labels
                xyPlot.setTicksPerRangeLabel(2);

                // Reduce the number of domain labels
                xyPlot.setTicksPerDomainLabel(3);

                // Remove all the developer guides from the chart
                xyPlot.disableAllMarkup();

 }

我想,我的价值观很好。但是当我按下显示时,导致我出现数组索引越界异常。

在我的数据库表中,只存储了一个值。

日志详细信息:

https://docs.google.com/file/d/0B_c-SDSO63obWV9RUjdzMnlzYms/edit?usp=sharing

https://docs.google.com/file/d/0B_c-SDSO63obQ0JvcEVpYWhFZG8/edit?usp=sharing

请帮忙

接受所有建议。

谢谢

尚卡尔

4

1 回答 1

0

你应该像这样使用光标

c.moveToFirst();

while(!c.isAfterLast()) 
{
   sugarlevels+=cursor.getString(cursor.getColumnIndex("sugarlevel"))+",";
  c.moveToNext();
}
于 2013-08-13T12:30:11.723 回答