2017-07-21 50 views
0

我是新來的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)); 

} 

} 
+0

看到需要'CursorLoader ' - onStartLoading'特別是 – pskink

+0

添加你的'DBHandler'和'MyCursorAdapter'類 –

+0

@OussemaAroua DBHandler非常大。我已經使用DBHandler來獲得遊標在UI線程上,它的工作原理。 DBHandler.getCursor()從數據庫加載遊標。我將上傳適配器 – ColonD

回答

0

onCreateLoader

loader = new DBLoader(this); 
return loader; 

把這個在onCreate方法,而不是onLoadFinished

myAdapter = new MyCursorAdapter(this,null,0); 
bookList.setAdapter(myAdapter); 
聲明你 DBLoader loader爲全局變量

把這個在onLoadFinishied

this.loader=(DBLoader)loader; 
adapter.changeCursor(cursor); 
+0

不是,那不是工作:( – ColonD

+0

作爲一個類變量是全局的嗎?我嘗試了它作爲一個普通的類變量和一個'static'。工作 – ColonD

+0

沒有錯誤日誌?嘗試記錄如果光標是空的或不是 –

0

經過一番搜索,我看到他們用 一個getSupportLoaderManager().initLoader(1,null,this).forceLoad();在互聯網上的幾個例子。這似乎工作。也在onCreateLoader()作品中致電loader.forceLoad()。經進一步檢查,我看到了這一點:https://issuetracker.google.com/issues/36925900

看來,我們必須實現在onStartLoading()forceLoad爲這樣:

protected void onStartLoading() { 
if (mCursor != null) { 
    deliverResult(mCursor); 
} 
if (takeContentChanged() || mCursor == null) { 
    forceLoad(); 
} 
} 

這樣做將停止對我們的活動.forceLoad()