Error when call 'qb.query(db, projection, selection, selectionArgs, null, null, orderBy);'
Posted
by
smalltalk1960s
on Stack Overflow
See other posts from Stack Overflow
or by smalltalk1960s
Published on 2011-01-03T04:45:29Z
Indexed on
2011/01/03
4:53 UTC
Read the original article
Hit count: 188
Hi all, I make a content provider named 'DictionaryProvider' (Based on NotepadProvider). When my program run to command 'qb.query(db, projection, selection, selectionArgs, null, null, orderBy);', error happen. I don't know how to fix. please help me. Below is my code
// file main calling DictionnaryProvider
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dictionary);
final String[] PROJECTION = new String[] {
DicColumns._ID, // 0
DicColumns.KEY_WORD, // 1
DicColumns.KEY_DEFINITION // 2
};
Cursor c = managedQuery(DicColumns.CONTENT_URI, PROJECTION, null, null, DicColumns.DEFAULT_SORT_ORDER);
String str = "";
if (c.moveToFirst()) {
int wordColumn = c.getColumnIndex("KEY_WORD");
int defColumn = c.getColumnIndex("KEY_DEFINITION");
do {
// Get the field values
str = "";
str += c.getString(wordColumn);
str +="\n";
str +=c.getString(defColumn);
} while (c.moveToNext());
}
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
// file DictionaryProvider.java
package com.example.helloandroid;
import java.util.HashMap;
import android.content.ContentProvider;
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;
import com.example.helloandroid.Dictionary.DicColumns;
public class DictionaryProvider extends ContentProvider {
//private static final String TAG = "DictionaryProvider";
private DictionaryOpenHelper dbdic;
static final int DATABASE_VERSION = 1;
static final String DICTIONARY_DATABASE_NAME = "dictionarydb";
static final String DICTIONARY_TABLE_NAME = "dictionary";
private static final UriMatcher sUriMatcher;
private static HashMap<String, String> sDicProjectionMap;
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
dbdic = new DictionaryOpenHelper(getContext(), DICTIONARY_DATABASE_NAME, null, DATABASE_VERSION);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(DICTIONARY_TABLE_NAME);
switch (sUriMatcher.match(uri)) {
case 1:
qb.setProjectionMap(sDicProjectionMap);
break;
case 2:
qb.setProjectionMap(sDicProjectionMap);
qb.appendWhere(DicColumns._ID + "=" + uri.getPathSegments().get(1));
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
// If no sort order is specified use the default
String orderBy;
if (TextUtils.isEmpty(sortOrder)) {
orderBy = DicColumns.DEFAULT_SORT_ORDER;
} else {
orderBy = sortOrder;
}
// Get the database and run the query
SQLiteDatabase db = dbdic.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
// Tell the cursor what uri to watch, so it knows when its source data changes
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
static {
sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(Dictionary.AUTHORITY, "dictionary", 1);
sUriMatcher.addURI(Dictionary.AUTHORITY, "dictionary/#", 2);
sDicProjectionMap = new HashMap<String, String>();
sDicProjectionMap.put(DicColumns._ID, DicColumns._ID);
sDicProjectionMap.put(DicColumns.KEY_WORD, DicColumns.KEY_WORD);
sDicProjectionMap.put(DicColumns.KEY_DEFINITION, DicColumns.KEY_DEFINITION);
// Support for Live Folders.
/*sLiveFolderProjectionMap = new HashMap<String, String>();
sLiveFolderProjectionMap.put(LiveFolders._ID, NoteColumns._ID + " AS " +
LiveFolders._ID);
sLiveFolderProjectionMap.put(LiveFolders.NAME, NoteColumns.TITLE + " AS " +
LiveFolders.NAME);*/
// Add more columns here for more robust Live Folders.
}
}
// file Dictionary.java
package com.example.helloandroid;
import android.net.Uri;
import android.provider.BaseColumns;
/**
* Convenience definitions for DictionaryProvider
*/
public final class Dictionary {
public static final String AUTHORITY = "com.example.helloandroid.provider.Dictionary";
// This class cannot be instantiated
private Dictionary() {}
/**
* Dictionary table
*/
public static final class DicColumns implements BaseColumns {
// This class cannot be instantiated
private DicColumns() {}
/**
* The content:// style URL for this table
*/
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/dictionary");
/**
* The MIME type of {@link #CONTENT_URI} providing a directory of notes.
*/
//public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.note";
/**
* The MIME type of a {@link #CONTENT_URI} sub-directory of a single note.
*/
//public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.note";
/**
* The default sort order for this table
*/
public static final String DEFAULT_SORT_ORDER = "modified DESC";
/**
* The key_word of the dictionary
* <P>Type: TEXT</P>
*/
public static final String KEY_WORD = "KEY_WORD";
/**
* The key_definition of word
* <P>Type: TEXT</P>
*/
public static final String KEY_DEFINITION = "KEY_DEFINITION";
}
}
thanks so much
© Stack Overflow or respective owner