0

我不确定为什么过滤不起作用。我见过很多例子,但游标非常特别。

我有一个基于数据库的一般项目列表。列是_ID、类别、标题和内容。

在类别下有两种类型:注意和待办事项。

所以,我的应用程序上有 3 个 ListView,一个用于所有项目,一个用于备注,另一个用于待办事项。

为了填充笔记列表,我有这个方法:

private void fillNotesData() {


    final ContentResolver resolver = getContentResolver();
    final String[] projection = { Notero_Table.COLUMN_ID, 
                                   Notero_Table.NOTE_CATEGORY,  
                                   Notero_Table.NOTE_TITLE,
                                   Notero_Table.NOTE_CONTENT };
    final String mSelectionArgs = "%Note%";
    mCursorForNotes = resolver.query(Notero_Provider.CONTENT_URI, 
                                    projection, 
                                    Notero_Table.NOTE_CATEGORY + " LIKE ?", 
                                    new String[] { mSelectionArgs }, 
                                    null);

    String[] from = new String[] { Notero_Table.NOTE_TITLE, Notero_Table.NOTE_CONTENT };
    int[] to = new int[] { R.id.text_row, R.id.text_row_two };

    getLoaderManager().initLoader(0, null, this);
    mAdapter = new SimpleCursorAdapter(this, 
                                        R.layout.row, 
                                        mCursorForNotes, 
                                        from, 
                                        to, 
                                        0);
    setListAdapter(mAdapter);
}

活动列表已创建,但根本没有过滤器。mCursorForNotes 的查询有问题。

有什么帮助吗?

谢谢。

4

1 回答 1

0

这是表类

public class Notero_Table {

private final static String TAG = "Notero_Table";

public static final String TABLE_NOTERO = "note";
public static final String COLUMN_ID = "_id";
public static final String NOTE_CATEGORY = "category";
public static final String NOTE_TITLE = "title";
public static final String NOTE_CONTENT = "content";

private static final String DATABASE_CREATE = "CREATE TABLE " 
                            + TABLE_NOTERO 
                            + "(" + COLUMN_ID + " integer primary key autoincrement, "
                                  + NOTE_CATEGORY + " text not null, "
                                  + NOTE_TITLE + " text not null, "
                                  + NOTE_CONTENT + " text not null " + ");";

public static void onCreate(SQLiteDatabase database) {

    database.execSQL(DATABASE_CREATE);
    Log.d(TAG, "***** Se crea base de datos *****");
}

public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    Log.w(NoteEdit.class.getName(), "Upgrading database version from version " + oldVersion 
            + " to " + newVersion + ", which will destroy all old data");
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTERO);
    onCreate(database);
}

}

这就是数据库 OpenHelper。

public class Notero_DBHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "notero.db";
private static final int DATABASE_VERSION = 3;

public Notero_DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
    Notero_Table.onCreate(database);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Notero_Table.onUpgrade(db, oldVersion, newVersion);

}

}

以防万一,这是提供者:

public class Notero_Provider extends ContentProvider {

private Notero_DBHelper database;
private static final int ALL_ITEMS = 10;
private static final int ITEM_ID = 20;


private static final String AUTHORITY = "com.dominicapps.notero.Notero_Provider";
private static final String BASE_PATH = "items";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/items";
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/item";

private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        sUriMatcher.addURI(AUTHORITY, BASE_PATH, ALL_ITEMS);
        sUriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", ITEM_ID);
}

@Override
public boolean onCreate() {
    database = new Notero_DBHelper(getContext());
    return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    CheckColumns(projection);
    queryBuilder.setTables(Notero_Table.TABLE_NOTERO);
    int UriType = sUriMatcher.match(uri);
    switch (UriType) {
    case ALL_ITEMS:
        break;
    case ITEM_ID:
        queryBuilder.appendWhere(Notero_Table.COLUMN_ID + "="
                + uri.getLastPathSegment());
        break;

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

    SQLiteDatabase db = database.getWritableDatabase();
    Cursor cursor = queryBuilder.query(db, projection, selection,
            selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;

}

@Override
public String getType(Uri uri) {
    return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    int uriType = sUriMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    int rowsDeleted = 0;
    long id = 0;
    switch (uriType) {
    case ALL_ITEMS:
        id = sqlDB.insert(Notero_Table.TABLE_NOTERO, null, values);
        break;
    default:
        throw new IllegalArgumentException("Uri desconocido: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return Uri.parse(BASE_PATH + "/" + id);
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    int uriType = sUriMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    int rowsDeleted = 0;
    switch (uriType) {
    case ALL_ITEMS:
        rowsDeleted = sqlDB.delete(Notero_Table.TABLE_NOTERO, selection,
                selectionArgs);
        break;
    case ITEM_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
            rowsDeleted = sqlDB.delete(Notero_Table.TABLE_NOTERO,
                    Notero_Table.COLUMN_ID + "=" + id, 
                    null);
        } else {
            rowsDeleted = sqlDB.delete(Notero_Table.TABLE_NOTERO, 
                    Notero_Table.COLUMN_ID + "=" + id + " and " + selection, 
                    selectionArgs);
        }
        break;
    default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsDeleted;
}

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

    int uriType = sUriMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    int rowsUpdated = 0;
    switch (uriType) {
    case ALL_ITEMS:
        rowsUpdated = sqlDB.update(Notero_Table.TABLE_NOTERO, 
                values, 
                selection,
                selectionArgs);
        break;
    case ITEM_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
            rowsUpdated = sqlDB.update(Notero_Table.TABLE_NOTERO, 
                    values,
                    Notero_Table.COLUMN_ID + "=" + id, 
                    null);
        } else {
            rowsUpdated = sqlDB.update(Notero_Table.TABLE_NOTERO, 
                    values,
                    Notero_Table.COLUMN_ID + "=" + id 
                    + "and" 
                    + selection,
                    selectionArgs);
        }
        break;
    default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsUpdated;
}

private void CheckColumns(String[] projection) {
    String[] available = { 
            Notero_Table.NOTE_CATEGORY,
            Notero_Table.NOTE_TITLE, 
            Notero_Table.NOTE_CONTENT,
            Notero_Table.COLUMN_ID };
    if (projection != null) {
        HashSet<String> requestedColumns = new HashSet<String>(
                Arrays.asList(available));
        HashSet<String> availableColumns = new HashSet<String>(
                Arrays.asList(available));
        if (!availableColumns.containsAll(requestedColumns)) {
            throw new IllegalArgumentException("Unknown columns in projection");
        }
    }
}
}

再次感谢。

于 2013-08-18T20:39:57.707 回答