使用SQLite保存数据并将其添加到ListView
我的问题是应用程序崩溃,它显示,不幸的是,应用程序已停止。使用SQLite保存数据并将其添加到ListView
MainActivity.java
public class MainActivity extends AppCompatActivity { DatabaseHelper myDB;
Button btnAdd;
Button btnList;
TextView tvView;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnList = (Button) findViewById(R.id.btnList);
tvView = (TextView) findViewById(R.id.Textview);
editText = (EditText) findViewById(R.id.editText);
myDB=new DatabaseHelper(this);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String newEntry = editText.getText().toString();
if (newEntry.length() != 0) {
AddData(newEntry);
editText.setText("");
} else {
Toast.makeText(MainActivity.this, "You must put something in the text field", Toast.LENGTH_LONG).show();
}
}
});
btnList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ListDataActivity.class);
startActivity(intent);
}
});
}
public void AddData(String newEntry) {
boolean insertData = myDB.addData(newEntry);
// check inserted successfully
if (insertData == true) {
Toast.makeText(MainActivity.this, "Successfully Entered Data!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
}
ListAcitivity.java
公共类ListDataActivity延伸AppCompatActivity {
DatabaseHelper myDB; ListView listView;
ArrayAdapter<String>listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
listView=(ListView)findViewById(R.id.listView);
myDB= new DatabaseHelper(this);
//populate an ArrayList<String> from the databases and then view it
ArrayList<String> theList=new ArrayList<>();
Cursor data=myDB.getListContent();
if(data.getCount()==0){
Toast.makeText(ListDataActivity.this,"The database was empty",Toast.LENGTH_LONG).show();
}else{
while(data.moveToNext()){
theList.add(data.getString(1));
listAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
}
DatabaseHelper.java
公共类DatabaseHelper延伸SQLiteOpenHelper {
public static final String DATABASE_NAME = "mylist.db"; public static final String TABLE_NAME = "mylist_data";
public static final String COL1 = "ID";
public static final String COL2 = "ITEM1";
public DatabaseHelper(Context context){
super (context, DATABASE_NAME, null , 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable="CREATE TABLE"+ TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT,"+
"ITEM1 TEXT)";
db.execSQL(createTable);
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXITS "+ TABLE_NAME);
onCreate(db);
db.close();
}
public boolean addData(String item1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item1);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as instered incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Return all the data from database
* @return
*/
public Cursor getListContent() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor data =db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return data;
}
}
logcat的节目
7月12日至27日:29:26.268 24636-24636/sg.edu.rp.c346.todolist E/AndroidRuntime :致命例外:main 进程:sg.edu.rp.c346.todolist,PID:24636 java.lang.IllegalStateException:尝试重新打开已关闭的对象:SQLiteDatabase:/data/data/sg.edu。 rp.c346.todolist/databases/mylist.db 在android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55) at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:520) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java :263) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) at sg.edu.rp.c346.todolist.DatabaseHelper.addData(DatabaseHelper.java:45) at sg.edu.rp .c346.todolist.MainActivity.AddData(MainActivity.java:58) at sg.edu.rp.c346.todolist.MainActivity $ 1.onClick(MainActivity.java:39) at android.view.View.performClick(View。 java:4438) at android.view.View $ PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java .lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) 12-27 07:29:28.078 24636-24636 /?我/过程:发送信号。 PID:24636 SIG:9
回答:
您不应该close()
SQLiteDatabase
给您SQLiteOpenHelper
onCreate()
和onUpgrade()
。只有你自己获得的close()
数据库句柄。
回答:
它看起来像你没有初始化你的myDB实例。
在尝试将条目添加到LocalDB之前,请在您的onCreate
方法的某处拨打myDB = new DatabaseHelper(this)
。
回答:
在访问它之前,您必须初始化myDB变量。在你的MainActivity.java中你没有初始化Helper类。
myDB= new DatabaseHelper(this);
只要做到这一点。 也修改这个条件
if (editText.length() != 0) { // Change this to newEntry.length() !=0 AddData(newEntry);
editText.setText("");
} else {
Toast.makeText(MainActivity.this, "You must put something in the text field", Toast.LENGTH_LONG).show();
}
希望这会起作用。
以上是 使用SQLite保存数据并将其添加到ListView 的全部内容, 来源链接: utcz.com/qa/264920.html