我做了從數據庫sqlite顯示數據的應用程序,但由於方法startManagingCursor和構造函數SimpleCursorAdapter不推薦使用,因此在運行或構建時不會運行應用程序。方法startManagingCursor和構造函數SimpleCursorAdapter已棄用
這裏我的代碼
DatabaseHelper.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 我desperatel你需要你的幫助,謝謝
請接受正確的答案,或者,至少,現有的評論。我們的同事需要時間並努力幫助您。 –