Reputation: 449
I made aplication for display data from database sqlite, but aplication not running when run or build because method startManagingCursor and constructor SimpleCursorAdapter is deprecated.
here my code
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;
}
}
is there something wrong with writing code in method startManagingCursor and constructor SimpleCursorAdapter I desperately need your help, thank you
Upvotes: 1
Views: 2068
Reputation: 622
First, startManagingCursor()
should still works. It is not ideal, in that it performs database I/O on the main application thread. In Android, "deprecated" generally means "we have something else that we think is better that we suggest you use". This normally means you should consider migrating to its replacement.
The Loader framework is asynchronous and event-driven. As opposed to the now deprecated method startManagingCursor()
and consutructor simpleCursorAdapter
.
If you are building NEW code you should normally never use deprecated APIs although this will depend on what version of Android you are targeting.
In short your method should still work even though deprecated, as your question was related to deprecation, I answered accordingly. If you want to fix the bug ( toget this code as is working) post the error logs and we can try and figure out why. Alternatively if you want to change your code have a look here for more information on Loaders
Upvotes: 2