我是新來的android和java,我試圖實現一個異步加載程序。但沒有數據正在加載。我在這裏概念上錯過了什麼嗎?使用AsyncTask加載SQLite數據,我做錯了什麼?
我的活動:
public class TestActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
ListView bookList;
MyCursorAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_library);
// dloader = new DBLoader(this);
//dloader.loadInBackground();
/* blah blah*/
getSupportLoaderManager().initLoader(1,null,this);
/* blah blah*/
View header = (View) getLayoutInflater().inflate(R.layout.row_layout_title, null);
TextView totals = header.findViewById(R.id.mainText);
totals.setText("Total Books: " + Integer.toString(totalBooks));
bookList.addHeaderView(header);
bookList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
/* blah blah*/
}
}
});
/* blah blah*/
}
/* blah blah*/
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
/*CursorLoader loader = new CursorLoader(this, null, null, null, null, null){
@Override
public Cursor loadInBackground(){
DBHandler db = new DBHandler(getContext());
return db.getCursor();
}
};*/
DBLoader loader = new DBLoader(this);
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
myAdapter = new MyCursorAdapter(this,data,0);
bookList.setAdapter(myAdapter);
// myAdapter.swapCursor(data);
//myAdapter.notifyDataSetChanged();
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
myAdapter.swapCursor(null);
}
}
DBLoader:
public class DBLoader extends AsyncTaskLoader<Cursor> {
private Cursor mCursor;
Context context;
public DBLoader(Context context) {
super(context);
this.context = context;
}
@Override
public Cursor loadInBackground() {
DBHandler db = new DBHandler(context);
Cursor cursor = db.getCursor();
return cursor;
}
@Override
public void deliverResult(Cursor cursor){
if(isReset()){
releaseResources(cursor);
return;
}
Cursor oldCursor = mCursor;
mCursor = cursor;
if(isStarted()){
super.deliverResult(cursor);
}
if(oldCursor!=null && oldCursor!=cursor){
releaseResources(oldCursor);
}
}
private void releaseResources(Cursor cursor) {
cursor.close();
}
}
我不知道如果我在這裏失去了任何重要來電,並且在互聯網上的大部分資源都有關內容供應商(其我不需要)。我只需要從我的本地SQLite數據庫異步加載數據。
編輯:我的適配器:
public class MyCursorAdapter extends CursorAdapter {
private LayoutInflater cursorInflater;
public MyCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
cursorInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return cursorInflater.inflate(R.layout.row_layout, parent, false);
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
Log.d("Cursor :", cursor.getString(1));
TextView title = (TextView) v.findViewById(R.id.listTitle);
title.setText(cursor.getString(1));
TextView author = (TextView) v.findViewById(R.id.listAuthor);
author.setText(cursor.getString(2));
TextView copies = (TextView) v.findViewById(R.id.listCopies);
copies.setText(cursor.getString(5));
}
}
看到需要'CursorLoader ' - onStartLoading'特別是 – pskink
添加你的'DBHandler'和'MyCursorAdapter'類 –
@OussemaAroua DBHandler非常大。我已經使用DBHandler來獲得遊標在UI線程上,它的工作原理。 DBHandler.getCursor()從數據庫加載遊標。我將上傳適配器 – ColonD