title: Access Android SQLite Database created in Main Activity from Second activity
body: <p>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".</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private DBOpenHelper tdb;
  private SQLiteDatabase sdb;
  private ListView mainlist;
  private ArrayList&lt;String&gt; al_strings;
  private ArrayAdapter&lt;String&gt; 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&lt;String&gt;();
      c.moveToFirst();
      for(int i = 0; i &lt; c.getCount(); i++) {
          al_strings.add(c.getString(0) + " " + c.getString(1));
          c.moveToNext();
      }
      aa_strings = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, al_strings);
      mainlist.setAdapter(aa_strings);
  
  
  }
  </code></pre>
  
  <p><strong>SecondActivity.java</strong></p>
  
  <pre><code>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();
          }
      });
  
  
  
  
      }
  }
  </code></pre>