I am trying to display information from my SQLite database in a chart. I am using the AchartEngine for this, but I cannot seem to get the chart to populate from the database. I have hard coded in in the example below to show that the chart draws, but no matter what I try, I cannot seem to get the values to plot on the chart. I am new to android and any help would be great.
Thanks.
Here is my DBAdapter Class:
public class DBAdapter {
// For logging:
private static final String TAG = "DBAdapter";
// DB Fields
public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;
public static final String KEY_EXERCISE = "exercise";
public static final String KEY_LAST = "lastCount";
public static final String KEY_HIGH = "highCount";
public static final int COL_EXERCISE = 1;
public static final int COL_LAST = 2;
public static final int COL_HIGH = 3;
public static final String[] ALL_KEYS = new String[] { KEY_ROWID, KEY_EXERCISE,
KEY_LAST, KEY_HIGH };
public static final String DATABASE_NAME = "Exercise.db";
public static final String DATABASE_TABLE = "CounterTable";
public static final int DATABASE_VERSION = 3;
private static final String CREATE_TABLE = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_EXERCISE
+ " TEXT NOT NULL, " + KEY_LAST + " TEXT NOT NULL, "
+ KEY_HIGH + " TEXT NOT NULL);";
// Context of application
private DatabaseHelper myDBHelper;
private final Context context;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}
// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
// Add a new set of values to the database.
public long insertRow(String exercise, int lastCount, int highCount) {
// Create row's data:
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_EXERCISE, exercise);
initialValues.put(KEY_LAST, lastCount);
initialValues.put(KEY_HIGH, highCount);
// Insert it into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Change an existing row to be equal to new data.
public void updateRow(long rowId, String exercise,
int lastCount, int highCount) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_EXERCISE, exercise);
newValues.put(KEY_LAST, lastCount);
newValues.put(KEY_HIGH, highCount);
// Insert it into the database.
db.update(DATABASE_TABLE, newValues, where, null);
}
// Change an existing row to be equal to new data.
public void updateRow(String exercise, int lastCount) {
String where = KEY_EXERCISE + "= \"" + exercise +"\"";
ContentValues newValues = new ContentValues();
newValues.put(KEY_LAST, lastCount);
// Insert it into the database.
db.update(DATABASE_TABLE, newValues, where, null);
}
// Change an existing row to be equal to new data.
public void updateRow(String exercise, int lastCount, int highCount) {
String where = KEY_EXERCISE + "= \"" + exercise +"\"";
ContentValues newValues = new ContentValues();
newValues.put(KEY_LAST, lastCount);
newValues.put(KEY_HIGH, highCount);
// Insert it into the database.
db.update(DATABASE_TABLE, newValues, where, null);
}
// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public Cursor getRow(String exercise) {
String where = KEY_EXERCISE + "= \"" + exercise +"\"";
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
/**
* Private class which handles database creation and upgrading. Used to
* handle low-level database access.
*/
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Recreate new database:
onCreate(_db);
}
}
}
Here is my ChartActivity:
public class BarChartActivity extends Circuit {
private Button BackButton;
private View mChart;
private String[] exercise = new Circuit().exercises;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw_bar_chart);
BackButton = (Button) this.findViewById(R.id.backButton);
// Set up quit button function
BackButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), ChartMenu.class);
startActivity(intent);
}
});
////////////////////////////////////////////****Problem Here****/////////////////////////////////////////////////////////////////////////////
int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
// What do i put in here to get the values to read from the database?
int[] Last = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
int[] Highest = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Creating an XYSeries for Last
XYSeries LastSeries = new XYSeries("Last");
// Creating an XYSeries for Highest
XYSeries HighestSeries = new XYSeries("Highest");
// Adding data to Last and Highest Series
for (int i = 0; i < x.length; i++) {
LastSeries.add(i, Last[i]);
HighestSeries.add(i, Highest[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Last Series to the dataset
dataset.addSeries(LastSeries);
// Adding Highest Series to dataset
dataset.addSeries(HighestSeries);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setChartTitle("Last vs Highest Chart");
multiRenderer.setXTitle("Exercises");
multiRenderer.setYTitle("Reps");
multiRenderer.setAxesColor(Color.BLACK);
multiRenderer.setLabelsColor(Color.BLACK);
multiRenderer.setMarginsColor(Color.WHITE);
multiRenderer.setBarSpacing(0.5);
multiRenderer.setYLabelsPadding(10);
multiRenderer.setMargins(new int[] { 5, 15, 5, 5 });
multiRenderer.setXLabelsAngle(300);
multiRenderer.setXLabelsPadding(20);
for (int i = 0; i < exercise.length - 1; i++) {
multiRenderer.addXTextLabel(i, exercise[i]);
}
// Creating XYSeriesRenderer to customize highestSeries
XYSeriesRenderer highestRenderer = new XYSeriesRenderer();
highestRenderer.setDisplayChartValues(true);
highestRenderer.setChartValuesSpacing((float) 2.5);
// Creating XYSeriesRenderer to customize lastSeries
XYSeriesRenderer lastRenderer = new XYSeriesRenderer();
lastRenderer.setColor(Color.RED);
lastRenderer.setDisplayChartValues(true);
lastRenderer.setChartValuesSpacing((float) 2.5);
// Adding LastRenderer and HighestRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to
// multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(lastRenderer);
multiRenderer.addSeriesRenderer(highestRenderer);
// this part is used to display graph on the xml
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart);
// remove any views before u paint the chart
chartContainer.removeAllViews();
// drawing bar chart
mChart = ChartFactory.getBarChartView(BarChartActivity.this, dataset,
multiRenderer, null);
// adding the view to the linearlayout
chartContainer.addView(mChart);
db.close();
}
}
}
I'm already using a simple cursor adapter to read from the db in another class to display the db in a listView:
public class LogView extends Activity {
private Button BackButton;
DBAdapter myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_db_list);
openDB();
populateListViewFromDB();
//registerListClickCallback();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void openDB() {
myDb = new DBAdapter(this);
myDb.open();
}
private void closeDB() {
myDb.close();
}
public void populateListViewFromDB() {
Cursor cursor = myDb.getAllRows();
// Allow activity to manage lifetime of the cursor.
// DEPRECATED! Runs on the UI thread, OK for small/short queries.
startManagingCursor(cursor);
// Setup mapping from cursor to view fields:
String[] fromFieldNames = new String[] { DBAdapter.KEY_ROWID, DBAdapter.KEY_EXERCISE,
DBAdapter.KEY_LAST, DBAdapter.KEY_HIGH,
DBAdapter.KEY_LAST };
int[] toViewIDs = new int[] { R.id.textView0, R.id.textView2, R.id.textView1,
R.id.textView3 };
// Create adapter to may columns of the DB onto elements in the UI.
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, // Context
R.layout.item_layout, // Row layout template
cursor, // cursor (set of DB records to map)
fromFieldNames, // DB Column names
toViewIDs // View IDs to put information in
);
// Set the adapter for the list view
ListView myList = (ListView) findViewById(R.id.list);
myList.setAdapter(myCursorAdapter);
BackButton = (Button) this.findViewById(R.id.backButton);
// Set up quit button function
BackButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), StartScreen.class); startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.log_view, menu);
return true;
}
}
Any help would be great, I'm really stuck. Thanks
Aucun commentaire:
Enregistrer un commentaire