こんにちは私は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;
}
}