访问从第二个活动在主活动中创建的Android SQLite数据库

时间:2017-02-28 12:21:36

标签: android sqlite android-intent android-activity android-sqlite

我正在尝试在主要活动中调用的第二个活动中,在主活动中创建的SQLite数据库中插入值。 我在创建一个新的数据库打开助手时会从上下文中得到错误,说“主要活动不是一个封闭的类”。

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();
        }
    });




    }
}

4 个答案:

答案 0 :(得分:1)

在此行中,您已将MainActivity用作上下文

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

在你的DBOpenHelper类中,构造函数param应该是Context,你应该传递

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

答案 1 :(得分:0)

在第二个活动中写下这个     db = new DBOpenHelper(this,&#34; contacts.db&#34;,null,1);

答案 2 :(得分:0)

建议:

为数据库操作创建单独的类,例如SQLiteHelper.class在完成所有数据库方法(如插入,删除和初始化)的情况下,通过传递此活动的上下文,例如

SQLiteHelper sqh = new SQLiteHelper(con); 

从单个班级多次调用所需的方法。

sqh.InsertIntoContacts(values);

答案 3 :(得分:0)

在你的第二项活动中,

将Context的对象声明为

Context context = this;

而不是

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

而不是这个,请像这样使用

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

这可以解决您的问题。我没有测试它。让我知道状态