我做了一個保存筆記的android應用程序。筆記確實被保存了。但是,當應用程序打開時,僅顯示示例註釋Akhilesh Chobey
。所有其他音符都顯示在在Main2Activity
(活動編輯注)按後退按鈕數據庫未在第一次打開應用程序時顯示
MainActivity:
public class MainActivity extends AppCompatActivity {
ListView notesListView;
static ArrayList<String> notesArrayList;
static ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notesListView = (ListView) findViewById(R.id.notesListView);
notesArrayList = new ArrayList<String>();
if(Main2Activity.myDb != null) {
notesArrayList.clear();
Cursor res = Main2Activity.myDb.getData();
if (res.getCount() == 0) {
Log.i("Error", "error");
return;
}
while (res.moveToNext()) {
notesArrayList.add(res.getString(res.getColumnIndex("text")));
}
}
notesArrayList.add("Akhilesh Chobey");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);
notesListView.setAdapter(adapter);
notesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
intent.putExtra("notePosition", i);
startActivity(intent);
}
});
}
}
Main2Activity:
public class Main2Activity extends AppCompatActivity implements TextWatcher {
static DatabaseOperations myDb;
EditText editNote;
int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editNote = (EditText) findViewById(R.id.noteEditText);
myDb = new DatabaseOperations(Main2Activity.this);
Intent intent = getIntent();
position = intent.getIntExtra("notePosition", -1);
if(position != -1){
editNote.setText(MainActivity.notesArrayList.get(position));
}
editNote.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (myDb != null) {
boolean isInserted = myDb.insertData(editNote.getText().toString());
MainActivity.notesArrayList.set(position, String.valueOf(charSequence));
MainActivity.adapter.notifyDataSetChanged();
}
}
數據庫助手類:
public class DatabaseOperations extends SQLiteOpenHelper {
public static final String DatabaseName = "notes.db";
public static final String TableName = "notes";
public static final String Col1 = "text";
public DatabaseOperations(Context context) {
super(context, DatabaseName, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TableName + " (text TEXT) ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TableName);
onCreate(db);
}
public boolean insertData(String note){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Col1, note);
long result = db.insert(TableName, null, contentValues);
if(result == -1){
return false;
}else {
return true;
}
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from " + TableName, null);
return result;
}
}
在Main2Activity類
從其他Activity獲取對db helper的引用對我而言,這看起來很奇怪。我認爲如果(Main2Activity.myDb!= null){Note:ArrayArray.clear();};}}。遊標res = Main2Activity.myDb.getData();遊標res = Main2Activity.myDb.getData();遊標res = Main2Activity.myDb.getData();如果(res.getCount()== 0){ Log.i(「Error」,「error」); return; } while(res.moveToNext())notesArrayList.add(res.getString(res.getColumnIndex(「text」))); } } 這是問題,db返回null,所以你不加載數據庫的日期。 – Rafal
無論如何,我可以使數據庫'myDb'「不爲空」? –
在Activity1中創建新實例 myDb = new DatabaseOperations(this); – Rafal