Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:30:55+00:00 2026-06-15T23:30:55+00:00

Hi I am new to android and I am writing the following code to

  • 0

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


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:30:55+00:00Added an answer on June 15, 2026 at 11:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code to display the values stored in Android sqlite database.
I have been writing apps for my Droid x2 using the new Android update
i am creating a new android application.i am using the table layout. I have
I created a sample application using android sqlite database, my requirements are How can
I am writing a program which employs RSA in Android. I have the following
I'm using the following code in my app, from tutorials etc., to get the
I'm new to android and am writing a simple app that calculates the distance
when i am creating new android project from visual studio 2010 it gives the
I am new to writing Android Applications, and I need to be able to
i'm new with Android and i'm writing a simple alarm clock application.when i type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.