2012-11-06 68 views
0

我填充使用SimpleCursorAdapterAndroid的 - 動態的ListView使用SimpleCursorAdapter

@Override 
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { 

    String[] projection = new String[] { BlogTable.TITLE_PLAIN, BlogTable.DATE_MODIFIED, BlogTable.EXCERPT, BlogTable.ID }; 

    CursorLoader loader = new CursorLoader(parent, BlogContentProvider.CONTENT_URI, projection, null, null, BlogTable.DATE_MODIFIED + " DESC LIMIT " + BlogContentProvider.QUERY_LIMIT); 

    return loader; 
} 

private void fillData(){ 

    //_id is expected from this method that is why we used it earlier 
    String[] from = new String[] { BlogTable.TITLE_PLAIN, BlogTable.DATE_MODIFIED, BlogTable.EXCERPT}; 
    int[] to = new int[] { R.id.text_news_title, R.id.text_news_date, R.id.text_news_excerpt}; 

    //initialize loader to call this class with a callback 
    getLoaderManager().initLoader(0, null, this); 

    //We create adapter to fill list with data, but we don't provide any data as it will be done by loader 
    adapter = new SimpleCursorAdapter(parent, R.layout.news_list_view, null, from, to, 0); 

    setListAdapter(adapter); 
} 

我希望每次只加載10個項目,然後在列表的末尾調用一個方法addData(一個ListView),並得到10多行等等。我知道這是CWAC EndlessAdapter,但是我不知道要向當前ListView添加另外10行並且同時保持位置。

我知道這可能聽起來像一個愚蠢的問題,但我相對較新的Android開發和仍在學習。誰能幫忙?

編輯:

這是我使用的ContentProvider的,也許它可以幫助

package com.brndwgn.database; 

import android.content.ContentProvider; 
import android.content.ContentResolver; 
import android.content.ContentValues; 
import android.content.UriMatcher; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteQueryBuilder; 
import android.net.Uri; 
import android.text.TextUtils; 

public class BlogContentProvider extends ContentProvider { 


    private DbHelper dbh; 

    //identifiers for URI types 
    public static final int BLOG_LIST = 1; 
    public static final int BLOG_ITEM = 2; 

    //elements of our URI to identify our COntentProvider 
    public static final String AUTHORITY = "com.brndwgn.database"; 
    public static final String BASE_PATH = "blog"; 


    //URI to query from this Content provider 
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH); 


    //MIME data types we offer 
    public static final String BLOG_LIST_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/bloglist"; 
    public static final String BLOG_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/blogitem"; 

    public static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); 
    //patterns for our provider 
    static { 
     matcher.addURI(AUTHORITY, BASE_PATH, BLOG_LIST); 
     matcher.addURI(AUTHORITY, BASE_PATH + "/#", BLOG_ITEM); 
    } 

    public static final int QUERY_LIMIT = 2; 

    @Override 
    public boolean onCreate() { 
     dbh = new DbHelper(getContext()); 
     return true; 
    } 

    @Override 
    public String getType(Uri uri) { 
     int uriId = matcher.match(uri); 
     //we check id of URI and return correct MIME type, we defined all of them before 
     switch(uriId) { 
     case BLOG_ITEM: return BLOG_ITEM_TYPE; 
     case BLOG_LIST: return BLOG_LIST_TYPE; 
     default: 
      throw new IllegalArgumentException("Unknown uri: " + uri); 
     } 
    } 

    @Override 
    public Cursor query(Uri uri, String[] projection, String selection, 
      String[] selectionArgs, String sortOrder) { 
     int uriId = matcher.match(uri); 

     //we create object of SQL query builder so we don't need to use plain SQL 
     SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); 

     //Set a name for the table to query 
     builder.setTables(BlogTable.TABLE_NAME); 

     switch(uriId) { 
     case BLOG_ITEM: 
      //set where condition to get just one row 
      builder.appendWhere(BlogTable.ID + "=" + uri.getLastPathSegment()); 
      break; 
     case BLOG_LIST: 
      //we don't need to do anything here 
      break; 
     default: 
      new IllegalArgumentException("Unknown uri: " + uri); 
     } 

     //get instance of database 
     SQLiteDatabase db = dbh.getReadableDatabase(); 

     //execute query 
     Cursor cursor = builder.query(db, projection, selection, selectionArgs, null, null, sortOrder); 

     //set notifications for this URI 
     cursor.setNotificationUri(getContext().getContentResolver(), uri); 

     return cursor; 
    } 

    @Override 
    public int delete(Uri uri, String selection, String[] selectionArgs) { 
     int uriId = matcher.match(uri); 

     int deleted=0; 

     SQLiteDatabase db = dbh.getWritableDatabase(); 
     switch(uriId) { 
     case BLOG_LIST: 
      deleted = db.delete(BlogTable.TABLE_NAME, selection, selectionArgs); 
      break; 
     case BLOG_ITEM: 
      if(TextUtils.isEmpty(selection)) { 
       deleted = db.delete(BlogTable.TABLE_NAME, BlogTable.ID + "=" + uri.getLastPathSegment(), selectionArgs); 
      } 
      else { 
       deleted = db.delete(BlogTable.TABLE_NAME, selection + " and " + BlogTable.ID + "=" + uri.getLastPathSegment(), selectionArgs); 
      } 
      break; 
     default: 
      throw new IllegalArgumentException("Unknow uri: " + uri); 
     } 
     getContext().getContentResolver().notifyChange(uri, null); 
     return deleted; 
    } 


    @Override 
    public Uri insert(Uri uri, ContentValues values) { 
     int uriId = matcher.match(uri); 

     //Variable for ID of new record 
     long newId; 

     switch(uriId) { 
     case BLOG_LIST: 
      //get instance of Database 
      SQLiteDatabase db = dbh.getWritableDatabase(); 

      //execute query 
      newId = db.replace(BlogTable.TABLE_NAME, null, values); 
      //newId = db.insertWithOnConflict(BlogTable.TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE); 

      //create URI for new added record 
      Uri newuri = Uri.parse(CONTENT_URI + "/" + newId); 

      //notify change for list URI 
      getContext().getContentResolver().notifyChange(uri, null); 

      return newuri; 

     default: 
      throw new IllegalArgumentException("Unknown uri: " + uri); 
     } 


    } 

    @Override 
    public int update(Uri uri, ContentValues values, String selection, 
      String[] selectionArgs) { 

     int uriId = matcher.match(uri); 

     int updated=0; 

     SQLiteDatabase db = dbh.getWritableDatabase(); 

     switch(uriId) { 
     case BLOG_LIST: 
      updated = db.update(BlogTable.TABLE_NAME, values, selection, selectionArgs); 
      break; 
     case BLOG_ITEM: 
      if(TextUtils.isEmpty(selection)) { 
       updated = db.update(BlogTable.TABLE_NAME, values, BlogTable.ID + "=" + uri.getLastPathSegment(), null); 
      } 
      else { 
       updated = db.update(BlogTable.TABLE_NAME, values, selection + " and " + BlogTable.ID + "=" + uri.getLastPathSegment(), selectionArgs); 
      } 
      break; 
     default: 
      throw new IllegalArgumentException("Unknown URI: " + uri); 
     } 
     getContext().getContentResolver().notifyChange(uri, null); 
     return updated; 
    } 

} 

回答

0

使用

adapter.notifyDataSetChanged(); 

其中adapter是你SimpleCursorAdapter爲ListView

編輯:

在此之前,您應該將數據添加到您的適配器。爲此,請添加一個適配器從數據庫查詢的方法。

+0

但是,在實際進行任何通知更改之前,我是否還需要調用數據庫來獲取下10行?添加了 – user1774438

+0

@ user1774438。 – onemach

+0

好的,謝謝你的答覆,但問題是,我不知道如何實現該方法,通過做一個新的查詢實際添加行到適配器。你能幫忙嗎? – user1774438

相關問題