这是我的MainActivity。关闭应用程序后不显示Sqlite数据
公共类MainActivity扩展活动{
EditText etName, etEmail;
DatabaseHelper dbHelper;
Button save;
// declare view
ListView lvEmployees;
// declare adapter
CustomizedAdapter adapter;
// datasource
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
etEmail = (EditText) findViewById(R.id.etEmail);
save = (Button) findViewById(R.id.btnSave);
lvEmployees = (ListView) findViewById(R.id.lvEmployees);
dbHelper = new DatabaseHelper(this);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
save(v);
}
});
}
public void save(View v) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
Employee employee = new Employee(name, email);
Toast.makeText(getApplicationContext(), employee.toString(),
Toast.LENGTH_LONG).show();
long inserted = dbHelper.insertEmployee(employee);
if (inserted >= 0) {
Toast.makeText(getApplicationContext(), "Data inserted",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Data insertion failed...",
Toast.LENGTH_LONG).show();
}
ArrayList<Employee> employees = dbHelper.getAllEmployees();
if (employees != null && employees.size() > 0) {
adapter = new CustomizedAdapter(this, employees);
lvEmployees.setAdapter(adapter);
}
}
}
这是我的DataBaseHelper。
公共类DatabaseHelper当我把数据和按下保存按钮,然后我的数据被保存并显示在我的ListView扩展SQLiteOpenHelper {
public static final String DB_NAME = "task_management";
public static final int DB_VERSION = 1;
public static final String EMPLOYEE_TABLE = "employee";
public static final String ID_FIELD = "_id";
public static final String NAME_FIELD = "name";
public static final String EMAIL_FIELD = "email";
public static final String EMPLOYEE_TABLE_SQL = "CREATE TABLE "
+ EMPLOYEE_TABLE + " (" + ID_FIELD + " INTEGER PRIMARY KEY, "
+ NAME_FIELD + " TEXT, " + EMAIL_FIELD + " DATETIME);";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// create tables
db.execSQL(EMPLOYEE_TABLE_SQL);
Log.e("TABLE CREATE", EMPLOYEE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// upgrade logic
}
// insert
public long insertEmployee(Employee emp) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_FIELD, emp.getName());
values.put(EMAIL_FIELD, emp.getEmail());
long inserted = db.insert(EMPLOYEE_TABLE, null, values);
db.close();
return inserted;
}
// query
public ArrayList<Employee> getAllEmployees() {
ArrayList<Employee> allEmployees = new ArrayList<Employee>();
SQLiteDatabase db = this.getReadableDatabase();
// String[] columns={NAME_FIELD, EMAIL_FIELD, PHONE_FIELD};
// SELECT * FROM EMPLOYEE;
Cursor cursor = db.query(EMPLOYEE_TABLE, null, null, null, null, null,
null);
// Cursor cursor = db.rawQuery("SELECT * FROM EMPLOYEE", null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
//
int id = cursor.getInt(cursor.getColumnIndex(ID_FIELD));
String name = cursor.getString(cursor
.getColumnIndex(NAME_FIELD));
String email = cursor.getString(cursor
.getColumnIndex(EMAIL_FIELD));
Employee e = new Employee(id, name, email);
allEmployees.add(e);
cursor.moveToNext();
}
}
cursor.close();
db.close();
return allEmployees;
}
}
。 但是,当我关闭应用程序并打开它,然后我没有看到我的ListView中的任何数据。 投入数据并按下保存按钮后,我的新数据和现有数据显示在我的ListView中。 那么如何在打开我的应用程序并且不按保存按钮后在ListView中显示我现有的数据。
如果我将此代码移到oncreate,那么如果我保存我的数据它不显示。 当我重新打开应用程序,然后显示。@ MysticMagic – 2014-09-27 17:54:53
@RasedurjamanSojib你读了P.S?我告诉你,你需要调用相同的代码,这两个地方..在onCreate和Save方法中,也是:) – 2014-09-27 17:55:51