0

SimpleCursorAdapter を使用して ListView にデータを入力しています

@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() を呼び出して、さらに 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;
    }

}
4

1 に答える 1

0

使用する

adapter.notifyDataSetChanged();

リストビューはどこにadapterありますかSimpleCursorAdapter

編集:

その前に、アダプタにデータを追加する必要があります。これを実現するには、アダプタがデータベースからクエリを実行するためのメソッドを追加します。

于 2012-11-06T09:01:35.313 に答える