使用数据库Android不积图绘制

使用数据库Android不积图绘制

本文介绍了使用数据库Android不积图绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来的Andr​​oid和我写了下面的code绘制柱状图从数据库中取的值。

我已经创建使用SQLite数据浏览器数据库。我的问题是,当我执行程序的图形亘古不情节。

感谢您的帮助提前。

 公共类ChartActivity延伸活动{
    私人字符串产品名称;
    私人语境贾扬;
    私人整数seriesCount;    公共意图执行(上下文的背景下)抛出IOException            共享preferences我的preFS = context.getShared preferences(我的preFS
                            MODE_PRIVATE);
            产品名称=我的prefs.getString(PRODNAME,无);
            贾扬特·=背景;
            DatabaseAdapter DBA =新DatabaseAdapter(贾扬);
            dbA.opendatabase();
            seriesCount = dbA.getValues​​(产品名称).getCount();
            dbA.close();            XYMultipleSeriesRenderer呈示= buildBarRender();
            setChartSettings(渲染);            返回ChartFactory.getBarChartIntent(上下文,getDateDemoDataset(),
                            渲染,Type.DEFAULT);
    }    保护XYMultipleSeriesRenderer buildBarRender(){
            XYMultipleSeriesRenderer渲染器=新XYMultipleSeriesRenderer();
            INT [] =颜色INT新[] {Color.BLUE,Color.DKGRAY,Color.GREEN,
                            Color.RED,Color.CYAN,};
            SimpleSeriesRenderer SSR =新SimpleSeriesRenderer();
            的for(int i = 0; I< seriesCount;我++){
                    SSR =新SimpleSeriesRenderer();
                    ssR.setDisplayChartValues​​(真);
                    ssR.setChartValues​​TextSize(30);
                    ssR.setColor(颜色[I]);
                    renderer.addSeriesRenderer(SSR);            }
            返回渲染器;
    }    私人XYMultipleSeriesDataset getDateDemoDataset()抛出IOException
            XYMultipleSeriesDataset数据=新XYMultipleSeriesDataset();
            DatabaseAdapter DBA =新DatabaseAdapter(贾扬);            dbA.opendatabase();
            cursor数据= dbA.getValues​​(产品名称);
            seriesCount = Data.getCount();            为(Data.move(-1); Data.moveToNext(); Data.isAfterLast()){
                    CategorySeries系列=新CategorySeries((Data.getString(数据
                                    .getColumnIndex(Primary1))));
                    series.add(Data.getInt(Data.getColumnIndex(Primary2)));
                    dataset.addSeries(series.toXYSeries());
            }
            Data.close();
            dbA.close();
            返回的数据集;
    }    私人无效setChartSettings(XYMultipleSeriesRenderer渲染器)抛出IOException
            renderer.setAxisTitleTextSize(16);
            renderer.setChartTitleTextSize(20);
            renderer.setLabelsTextSize(15);
            renderer.setLegendTextSize(15);
            renderer.setMargins(新INT [] {20,30,15,0});
            renderer.setChartTitle(十大旅游目的地);
            renderer.setXTitle(目标);
            renderer.setYTitle(十佳);
            renderer.setXAxisMin(0);
            renderer.setXAxisMax(10.5);
            renderer.setBarSpacing(0.0);
            renderer.setYAxisMin(0);
            renderer.setYAxisMax(maxValue分别());    }    私人双maxValue(最大值)()抛出IOException
            双mValue = NULL;
            DatabaseAdapter DBA =新DatabaseAdapter(贾扬);
            dbA.opendatabase();
            光标毫伏= dbA.getMaxValue(产品名称);
            如果(mV.moveToFirst())
                    mValue = mV.getInt(mV.getColumnIndex(Primary2))* 1.1;            mV.close();
            dbA.close();
            返回mValue;    }
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_chart);
    }}

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

 公共类DatabaseAdapter扩展SQLiteOpenHelper {    私人语境mycontext;    私有String = DB_PATH mycontext.getApplicationContext()getPackageName()+贾扬。
    私人静态字符串DB_NAME =Jayant.sqlite; //扩展名可能是.sqlite或.db的
    公共SQLiteDatabase MYDATABASE;    公共DatabaseAdapter(上下文的背景下)抛出IOException
        超级(上下文,DB_NAME,空,1);
        this.mycontext =背景;
        布尔dbexist = checkdatabase();
        如果(dbexist){
            的openDatabase();
        }其他{
            的System.out.println(数据库不存在);
            的CreateDatabase();
        }
    }    公共无效的CreateDatabase()抛出IOException
        布尔dbexist = checkdatabase();
        如果(dbexist){
        }其他{
            this.getReadableDatabase();
            尝试{
                copydatabase();
            }赶上(IOException异常五){
                抛出新的错误(错误复制数据库);
            }
        }
    }    私人布尔checkdatabase(){
        // SQLiteDatabase CHECKDB = NULL;
        布尔CHECKDB = FALSE;
        尝试{
            字符串mypath中= DB_PATH + DB_NAME;
            文件DBFILE =新的文件(mypath中);
            // CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READWRITE);
            CHECKDB = dbfile.exists();
        }赶上(SQLiteException E){
            的System.out.println(数据库不存在);
        }
        返回CHECKDB;
    }    私人无效copydatabase()抛出IOException
        //打开本地数据库的输入流
        InputStream的myinput = mycontext.getAssets()开(DB_NAME)。        //路径刚刚创建的空分贝
        @燮pressWarnings(未使用)
        字符串outfilename = DB_PATH + DB_NAME;        //打开空分贝的输出流
        的OutputStream myoutput =新的FileOutputStream(/数据/数据​​/ flu.solutions.travelsense /数据库/Jayant.sqlite);        //传输字节到INPUTFILE到OUTPUTFILE
        字节[]缓冲区=新的字节[1024];
        INT长;
        而((长度= myinput.read(缓冲液))大于0){
            myoutput.write(缓冲液,0,长度);
        }        //关闭流
        myoutput.flush();
        myoutput.close();
        myinput.close();
    }    公共无效的openDatabase()抛出的SQLException {
        //打开数据库
        字符串mypath中= DB_PATH + DB_NAME;
        MYDATABASE = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READWRITE);
    }    公共同步无效的close(){
        如果(MYDATABASE!= NULL){
            myDataBase.close();
        }
        super.close();
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase为arg0){
        // TODO自动生成方法存根    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase为arg0,ARG1 INT,INT ARG2){
        // TODO自动生成方法存根    }    公共光标的GetValues​​(字符串产品名称){
        // TODO自动生成方法存根
        返回null;
    }    公共光标getMaxValue(字符串产品名称){
        // TODO自动生成方法存根
        返回null;
    }
}


解决方案

您正在创建图表,它看起来像它应该(据我所知)。但我无法找到你的图形添加到视图的地方。不应该有某种 findViewById ,你所创建的图形添加到视图。或者至少说,到Android,它应该吸取查看图表的地方,但即使我无法找到。

我没有很多的经验与Android中创建图表虽然。

Hi I am new to android and I am writing the following code to draw a bar graph taking the values from the database.

I have created the database using sqlite data browser. My problem is that when I execute the program the graph doesnot plot.

Thanks for your help in advance.

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 class to get the data from the database stored in the assets folder.

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


}
解决方案

You are creating a graph, which looks like it should (as far as I know). But I cannot find the place where you add the graph to the view. Shouldn't there be some kind findViewById where you add the created graph to the view. Or at least say to Android that it should draw the chart on the view somewhere, but even that I cannot find.

I don't have a lot of experience with creating charts in Android though.

这篇关于使用数据库Android不积图绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:08