Having issue with displaying the Data from a second Activity.
when I run the method getData form the second Activity it will crash.
Class Dabase
public static final String KEY_ROWID = "ExpenseID";
public static final String KEY_Expense = "ExpenseDescription";
public static final String KEY_Amount = "Amount";
private static final String DATABASE_NAME = "CSolutionsLlcBudget";
private static final String DATABASE_TABLE = "Expense";
private static final int DATABASE_VERSION = 4;
private static final String CREATE_TABLE_1 = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_Expense + "TEXT NOT NULL, " + KEY_Amount + "TEXT NOT NULL);";
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE_1);
}//end of onCreate
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}//end of class
public BudgetInfo(Context c){
ourContext = c;
}
public BudgetInfo open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String expense, String amount) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_Expense, expense);
cv.put(KEY_Amount, amount);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_Expense, KEY_Amount};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iExpense = c.getColumnIndex(KEY_Expense);
int iAmount = c.getColumnIndex(KEY_Amount);
for(c.moveToFirst();!c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iExpense) + " " + c.getString(iAmount) + "\n";
}
return result;
}
Second Activity
public class SQLView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
BudgetInfo info = new BudgetInfo(this);
info.open();
String data = info.getData();
tv.setText(data);
info.close();
}
}
The app crashes every time I click to view the second Activity
Here a few things u gotto try first. I am assuming you are inserting data into ure table in a previous activity.
Check that your database is getting created properly.
Check that you have data in your database.
Also check that your cursor is not null and returns true for movetofirst(). ( This you must do even if you get your solution from other posts).
Always close the cursor after you are done with it. I don’t see u r doing it here.