2012-12-17 125 views
-1

您好我是android新手,我正在编写下面的代码来绘制一个从数据库中获取值的条形图。使用数据库绘制的图不会在Android中绘制

我已经使用sqlite数据浏览器创建了数据库。我的问题是,当我执行程序时,图形不会绘制。

感谢您的帮助提前。

public class ChartActivity extends Activity { 
    private String productName; 
    private Context Jayant; 
    private Integer seriesCount; 

    public Intent execute(Context context) throws IOException { 

      SharedPreferences myPrefs = context.getSharedPreferences("myPrefs", 
          MODE_PRIVATE); 
      productName = myPrefs.getString("prodName", "nothing"); 
      Jayant = context; 
      DatabaseAdapter dbA = new DatabaseAdapter(Jayant); 
      dbA.opendatabase(); 
      seriesCount = dbA.getValues(productName).getCount(); 
      dbA.close(); 

      XYMultipleSeriesRenderer renderer = buildBarRender(); 
      setChartSettings(renderer); 

      return ChartFactory.getBarChartIntent(context, getDateDemoDataset(), 
          renderer, Type.DEFAULT); 
    } 

    protected XYMultipleSeriesRenderer buildBarRender() { 
      XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); 
      int[] colors = new int[] { Color.BLUE, Color.DKGRAY, Color.GREEN, 
          Color.RED, Color.CYAN, }; 
      SimpleSeriesRenderer ssR = new SimpleSeriesRenderer(); 
      for (int i = 0; i < seriesCount; i++) { 
        ssR = new SimpleSeriesRenderer(); 
        ssR.setDisplayChartValues(true); 
        ssR.setChartValuesTextSize(30); 
        ssR.setColor(colors[i]); 
        renderer.addSeriesRenderer(ssR); 

      } 
      return renderer; 
    } 

    private XYMultipleSeriesDataset getDateDemoDataset() throws IOException { 
      XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 
      DatabaseAdapter dbA = new DatabaseAdapter(Jayant); 

      dbA.opendatabase(); 
      Cursor Data = dbA.getValues(productName); 
      seriesCount = Data.getCount(); 

      for (Data.move(-1); Data.moveToNext(); Data.isAfterLast()) { 
        CategorySeries series = new CategorySeries((Data.getString(Data 
            .getColumnIndex("Primary1")))); 
        series.add(Data.getInt(Data.getColumnIndex("Primary2"))); 
        dataset.addSeries(series.toXYSeries()); 
      } 
      Data.close(); 
      dbA.close(); 
      return dataset; 
    } 

    private void setChartSettings(XYMultipleSeriesRenderer renderer) throws IOException { 
      renderer.setAxisTitleTextSize(16); 
      renderer.setChartTitleTextSize(20); 
      renderer.setLabelsTextSize(15); 
      renderer.setLegendTextSize(15); 
      renderer.setMargins(new int[] { 20, 30, 15, 0 }); 
      renderer.setChartTitle("Top 10 Travel Destinations"); 
      renderer.setXTitle("Destinations"); 
      renderer.setYTitle("Top 10"); 
      renderer.setXAxisMin(0); 
      renderer.setXAxisMax(10.5); 
      renderer.setBarSpacing(0.0); 
      renderer.setYAxisMin(0); 
      renderer.setYAxisMax(maxValue()); 

    } 

    private Double maxValue() throws IOException { 
      Double mValue = null; 
      DatabaseAdapter dbA = new DatabaseAdapter(Jayant); 
      dbA.opendatabase(); 
      Cursor mV = dbA.getMaxValue(productName); 
      if (mV.moveToFirst()) 
        mValue = mV.getInt(mV.getColumnIndex("Primary2")) * 1.1; 

      mV.close(); 
      dbA.close(); 
      return mValue; 

    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_chart); 


    } 

} 

DatabaseAdapter类从存储在资产文件夹中的数据库获取数据。

public class DatabaseAdapter extends SQLiteOpenHelper{ 

    private Context mycontext; 

    private String DB_PATH = mycontext.getApplicationContext().getPackageName()+"Jayant"; 
    private static String DB_NAME = "Jayant.sqlite";//the extension may be .sqlite or .db 
    public SQLiteDatabase myDataBase; 

    public DatabaseAdapter(Context context) throws IOException { 
     super(context,DB_NAME,null,1); 
     this.mycontext=context; 
     boolean dbexist = checkdatabase(); 
     if (dbexist) { 
      opendatabase(); 
     } else { 
      System.out.println("Database doesn't exist"); 
      createdatabase(); 
     } 
    } 

    public void createdatabase() throws IOException { 
     boolean dbexist = checkdatabase(); 
     if(dbexist) { 
     } else { 
      this.getReadableDatabase(); 
      try { 
       copydatabase(); 
      } catch(IOException e) { 
       throw new Error("Error copying database"); 
      } 
     } 
    } 

    private boolean checkdatabase() { 
     //SQLiteDatabase checkdb = null; 
     boolean checkdb = false; 
     try { 
      String myPath = DB_PATH + DB_NAME; 
      File dbfile = new File(myPath); 
      //checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE); 
      checkdb = dbfile.exists(); 
     } catch(SQLiteException e) { 
      System.out.println("Database doesn't exist"); 
     } 
     return checkdb; 
    } 

    private void copydatabase() throws IOException { 
     //Open your local db as the input stream 
     InputStream myinput = mycontext.getAssets().open(DB_NAME); 

     // Path to the just created empty db 
     @SuppressWarnings("unused") 
     String outfilename = DB_PATH + DB_NAME; 

     //Open the empty db as the output stream 
     OutputStream myoutput = new FileOutputStream("/data/data/flu.solutions.travelsense/databases /Jayant.sqlite"); 

     // transfer byte to inputfile to outputfile 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myinput.read(buffer))>0) { 
      myoutput.write(buffer,0,length); 
     } 

     //Close the streams 
     myoutput.flush(); 
     myoutput.close(); 
     myinput.close(); 
    } 

    public void opendatabase() throws SQLException { 
     //Open the database 
     String mypath = DB_PATH + DB_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE); 
    } 

    public synchronized void close() { 
     if(myDataBase != null) { 
      myDataBase.close(); 
     } 
     super.close(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
     // TODO Auto-generated method stub 

    } 

    public Cursor getValues(String productName) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public Cursor getMaxValue(String productName) { 
     // TODO Auto-generated method stub 
     return null; 
    } 


} 
+1

@ndsmyter你好,你知道我犯了什么错误吗? – user1844638

+2

我真的很想回答你的问题,但我不知道。但是我没有很多在Android中绘制图表的经验。 – ndsmyter

+1

好的谢谢.... :) – user1844638

回答

1

您正在创建一个图表,看起来应该如此(据我所知)。但我无法找到将图形添加到视图的位置。不应该有某种findViewById您将创建的图添加到视图。或者至少对Android说,它应该在视图上绘制图表,但即使是我找不到。

虽然我在Android中创建图表的经验并不多。

+1

我dnt知道,即使我没有很多经验在android – user1844638

+1

嘿@ndsmyter我想我不是正确地调用方法。 – user1844638