我为从数据库 sqlite 显示数据创建了应用程序,但是在运行或构建时应用程序没有运行,因为方法 startManagingCursor 和构造函数 SimpleCursorAdapter 已被弃用。
这是我的代码
数据库助手.java
package latihan.listviewsqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "dbanime";
private static final String TABLE = "ANIME";
public static final String NAMA = "nama";
public static final String KEY_ID = "_id";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, 1);
}
public void createTable(SQLiteDatabase db){
db.execSQL("DROP TABLE IF EXIST ANIME");
db.execSQL("CREATE TABLE if not exists ANIME (_id INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT");
}
public void generateData(SQLiteDatabase db){
ContentValues cv = new ContentValues();
cv.put(NAMA, "Naruto Shipuuden");
db.insert(TABLE, NAMA, cv);
cv.put(NAMA, "One Piece");
db.insert(TABLE, NAMA, cv);
cv.put(NAMA, "Bleach");
db.insert(TABLE, NAMA, cv);
}
public void deleteAllData(SQLiteDatabase db){
db.delete(TABLE, null, null);
}
public Cursor fetchAllAnime(SQLiteDatabase db){
return db.query(false, TABLE, new String[] {KEY_ID, NAMA}, null, null, null, null, null, null);
}
public void onCreate(SQLiteDatabase db){
createTable(db);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
}
MainActivity.java
package latihan.listviewsqlite;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
private DatabaseHelper dbHelper;
private SQLiteDatabase db = null;
private ListView listContent = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new DatabaseHelper(this);
db = dbHelper.getWritableDatabase();
dbHelper.deleteAllData(db);
dbHelper.generateData(db);
setContentView(R.layout.activity_main);
listContent = (ListView) findViewById(R.id.animeList);
isDataListView();
// setContentView(R.layout.activity_main);
}
private void isDataListView(){
Cursor animeCursor;
animeCursor = dbHelper.fetchAllAnime(db);
startManagingCursor(animeCursor);
String[] from = new String[]{dbHelper.NAMA};
int[] to = new int[]{R.id.txtAnime};
SimpleCursorAdapter animeAdapter = new SimpleCursorAdapter(this, R.layout.row, animeCursor, from, to);
listContent.setAdapter(animeAdapter);
}
public void onDestroy(){
super.onDestroy();
try{
db.close();
}catch (Exception ex){
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
在方法 startManagingCursor 和构造函数 SimpleCursorAdapter 中编写代码有什么问题吗?我非常需要你的帮助,谢谢