LoadManager,SimpleCursorAdapter,ViewBinder - 没有URI?

时间:2014-07-06 07:32:37

标签: android android-listview simplecursoradapter android-loadermanager android-viewbinder

我正在尝试实现LoaderManager,因为它花了一段时间将行加载到我的ListView中。我在网上找到的所有例子都指的是CONTENT_URI - 这是我没有的。

这是我加载ListView内容的旧代码:

  @Override
  public void onCreate( Bundle savedInstanceState )
  {
  ...
    String path = "/sdcard/mytunesdb.sqlite";   // Warning, yes, I know..

    db = SQLiteDatabase.openDatabase( path, null, 0 );

    lvCustomList = (ListView) findViewById( R.id.lv_custom_list );

    String[] columns = new String[] { "Name" };

    // THE XML DEFINED VIEWS FOR EACH FIELD TO BE BOUND TO
    int[] to = new int[] { R.id.lv_tune };  

    Cursor c = db.rawQuery( "SELECT rowid _id, Name FROM Tune ORDER BY Name", null );

    cAdapter = new MySimpleCursorAdapter( this, R.layout.like_hate_row, c, columns, to, 0 );

    viewBinder = new CustomViewBinder();

    cAdapter.setViewBinder( (ViewBinder) viewBinder );

    lvCustomList.setAdapter( cAdapter );

好的,所以我在更改上面的代码后开始实现LoaderManager回调,以便使用null projection(null而不是'columns'参数)创建cAdapter。这就是我得到的方式..如何将我的rawQuery放入onCreateLoader方法?

  @Override
  public Loader<Cursor> onCreateLoader(int id, Bundle args) 
  {
    String[] columns = new String[] { "Name" };

!!! HELP - I don't have a CONTENT_URI - my database is external - what do I put below?   
    CursorLoader cursorLoader = new CursorLoader( getActivity(),
              TutListProvider.CONTENT_URI, columns, null, null, null);

     return cursorLoader;
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) 
  {
    cAdapter.swapCursor( cursor );
  }

  @Override
  public void onLoaderReset(Loader<Cursor> loader) 
  {
    cAdapter.swapCursor( null );
  }

我是否需要保留URI代码并为我的SQL数据库实现ContentProvider?

1 个答案:

答案 0 :(得分:0)

我在其他StackOverlow帖子中提到的SimpleCursorLoader的帮助下想到了这一点。我有新问题,但在下面的链接中你会找到我的解决方案。

My Solution