I have 2 text fields on the screen and a button. When the button is pressed I want the information in the name text field to be passed to a variable, then run my addAccount() method in my DatabaseHelper.class, passing the variable into this method, this should store the variable information in an SQLite database.
Here is my Activity with the text field and button.
package mr.mwod.moneyorganiser;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddAccountActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addaccountlayout);
Button addAccountVairiable = (Button) findViewById(R.id.addAccountButton);
addAccountVairiable.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText accountNameData = (EditText)findViewById(R.id.accountNameTextField);
String accountNameVairiable = accountNameData.getText().toString();
//Need the code here to run addAccount form DatabaseHelper.class
//accountName vairiable needs to be passed into the method as the information to store.
}
});
}
}
I have commented where I think the code needs to go and what it needs to do.
Here is my DatabaseHelper.class encase their are errors is in there.
package mr.mwod.moneyorganiser;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "money_organiser";
private static final String TABLE_ACCOUNTS = "accounts";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "account_name";
private static final String KEY_ACCOUNT_BALANCE = "account_balance";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ACCOUNTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_ACCOUNT_BALANCE + " INTEGER" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCOUNTS);
// Create tables again
onCreate(db);
}
public void addAccount(String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
db.insert(TABLE_ACCOUNTS, null, values);
db.close();
}
}
Any help would be greatly appreciated.
Is there any trick in your question? Isn’t just calling
new DatabaseHelper(this).addAccount(accountNameVairiable);working? Of course it will make sense to reuse the variable, rather than allocating it every time, but I am almost sure this is not your problem.By the way I would recommend you to change the name of the primary key column of the table. Use _id, some android helper methods will be easier to use in this way.
EDIT: I just noticed the call happens in anonymous class. This as noted by you will trigger a compilation error, because the listener is not a Context. The activity is. Luckily this can be very easily solved via placing what class exactly to use for the ocnstruction (using the parent Activity, which is a context).