2

I'm trying to insert values in a SQLite Database created in my main activity, from a second activity called by the main. The error i get come from the context when creating a new database open helper, saying that "Main activity is not an enclosing class".

MainActivity.java

public class MainActivity extends AppCompatActivity { private DBOpenHelper tdb; private SQLiteDatabase sdb; private ListView mainlist; private ArrayList<String> al_strings; private ArrayAdapter<String> aa_strings; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tdb.close(); Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } }); tdb = new DBOpenHelper(this, "contacts.db", null, 1); sdb = tdb.getWritableDatabase(); String table_name = "contacts"; String[] columns = {"FIRST_NAME", "LAST_NAME"}; String where = null; String where_args[] = null; String group_by = null; String having = null; String order_by = null; Cursor c = sdb.query(table_name, columns, where, where_args, group_by, having, order_by); mainlist = (ListView) findViewById(R.id.mainlist); al_strings = new ArrayList<String>(); c.moveToFirst(); for(int i = 0; i < c.getCount(); i++) { al_strings.add(c.getString(0) + " " + c.getString(1)); c.moveToNext(); } aa_strings = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al_strings); mainlist.setAdapter(aa_strings); } 

SecondActivity.java

public class SecondActivity extends Activity { private DBOpenHelper tdb; private SQLiteDatabase sdb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Button cancel = (Button) findViewById(R.id.cancel_button); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); Button add = (Button) findViewById(R.id.add_button); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText first_name = (EditText) findViewById(R.id.et_first_name); EditText last_name = (EditText) findViewById(R.id.et_last_name); EditText home_phone = (EditText) findViewById(R.id.et_home_phone); EditText mobile_phone = (EditText) findViewById(R.id.et_mobile_phone); EditText email = (EditText) findViewById(R.id.et_email); tdb = new DBOpenHelper(MainActivity.this, "contacts.db", null, 1); sdb = tdb.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("FIRST_NAME", first_name.getText().toString()); cv.put("LAST_NAME", last_name.getText().toString()); cv.put("HOME_PHONE", home_phone.getText().toString()); cv.put("MOBILE_PHONE", mobile_phone.getText().toString()); cv.put("EMAIL", email.getText().toString()); sdb.insert("contacts", null, cv); al_strings.add(first_name.getText().toString() + " " + last_name.getText().toString()); aa_strings.notifyDataSetChanged(); finish(); } }); } } 
1

4 Answers 4

1

In this line you have used MainActivity as context

 tdb = new DBOpenHelper(MainActivity.this, "contacts.db", null, 1); 

In you DBOpenHelper class contructor param should be Context and you should pass

 tdb = new DBOpenHelper(getApplicationContext(), "contacts.db", null, 1); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it seems to work with getApplicationContext() !
0

write this in your second activity db = new DBOpenHelper(this, "contacts.db", null, 1);

3 Comments

'this ' wont work since, this refers to the OnClickListener
I get another error, saying that the DBOpenhelper cannot be applied to the context, as it is an "anonymous...view.view.OnClickListener"
then make it SecondActivity.this
0

In your second activity,

Declare an object of the Context as,

Context context = this;

and instead of

tdb = new DBOpenHelper(MainActivity.this, "contacts.db", null, 1);

instead of this, use like this

tdb = new DBOpenHelper(context, "contacts.db", null, 1);

This may solve your issue. I didn't test it though. Let me know the status also

Comments

0

A Suggestion :

Create A Separate Class For Database Actions, For Example SQLiteHelper.class Where all the Database Methods like insertion, deletion and Initialization are done, then call this class in any activity by passing context of this activity for example

SQLiteHelper sqh = new SQLiteHelper(con); 

Call the required Methods multiple times from single class.

sqh.InsertIntoContacts(values); 

3 Comments

My "DBOpenhelper" is already a separated class for database actions. In fact i cannot create a new helper, getting an error with the context parameter.
then in "DBOpenHelper" make database Methods there, and call them from any activity like myDBOpenhelper.Insert(values);
Secondly for context, In Activity Make Context variable Global, initialize it on onCreate of the activity like mContext = con, and use this mContext in DBOpenHelper(mContext , "contacts.db", null, 1);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.