-1

嗨,我是 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 类从存储在 assets 文件夹中的数据库中获取数据。

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;
    }


}
4

1 回答 1

1

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

不过,我没有太多在 Android 中创建图表的经验。

于 2012-12-17T11:24:54.143 回答