我在显示我自己制作的自定义ContentProvider返回的项目时遇到了这个问题....以下是自定义内容提供程序(SolutionProvider.java):
package pioperation.tregliapi.computercomesifa;
import java.util.HashMap;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
public class SolutionProvider extends ContentProvider {
static final String PROVIDER_NAME = "pioperation.tregliapi.computercomesifa.SolutionProvider";
static final String URL = "content://" + PROVIDER_NAME + "/solutions";
static final Uri CONTENT_URI = Uri.parse(URL);
static final String _ID = "_id";
static final String NAME = "name";
static final String SHORT_DESCRIPTION = "short_description";
static final String IMAGE_SMALL = "imageSmall";
private static HashMap<String, String> SOLUTIONS_PROJECTION_MAP;
static final int SOLUTIONS = 1;
static final int SOLUTION_ID = 2;
static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "solutions", SOLUTIONS);
uriMatcher.addURI(PROVIDER_NAME, "students/#", SOLUTION_ID);
}
/**
* Database specific constant declarations
*/
private SQLiteDatabase db;
static final String DATABASE_NAME = "Solutions";
static final String SOLUTIONS_TABLE_NAME = "solutions";
static final int DATABASE_VERSION = 1;
static final String CREATE_DB_TABLE =
" CREATE TABLE " + SOLUTIONS_TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT NOT NULL, " +
SHORT_DESCRIPTION +" TEXT NOT NULL, " +
IMAGE_SMALL +" TEXT NOT NULL );";
/**
* Helper class that actually creates and manages
* the provider's underlying data repository.
*/
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_DB_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + SOLUTIONS_TABLE_NAME);
onCreate(db);
}
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
/**
* Create a write able database which will trigger its
* creation if it doesn't already exist.
*/
db = dbHelper.getWritableDatabase();
System.out.print("il valore di DB è: " + db);
return (db == null)? false:true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
/**
* Add a new solution record
*/
long rowID = db.insert( SOLUTIONS_TABLE_NAME, "", values);
/**
* If record is added successfully
*/
if (rowID > 0)
{
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to add a record into " + uri);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(SOLUTIONS_TABLE_NAME);
String query = uri.getLastPathSegment().toLowerCase();
/*
switch (uriMatcher.match(uri)) {
case SOLUTIONS:
qb.setProjectionMap(SOLUTIONS_PROJECTION_MAP);
break;
case SOLUTION_ID:
qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1));
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (sortOrder == null || sortOrder == ""){
/**
* By default sort on student names
sortOrder = NAME;
// }
*/
Cursor c = qb.query(db, projection, selection, selectionArgs,
null, null, null);
/**
* register to watch a content URI for changes
*/
c.setNotificationUri(getContext().getContentResolver(), uri);
int totalRow = c.getCount();
// return c;
return c;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case SOLUTIONS:
count = db.delete(SOLUTIONS_TABLE_NAME, selection, selectionArgs);
break;
case SOLUTION_ID:
String id = uri.getPathSegments().get(1);
count = db.delete( SOLUTIONS_TABLE_NAME, _ID + " = " + id +
(!TextUtils.isEmpty(selection) ? " AND (" +
selection + ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case SOLUTIONS:
count = db.update(SOLUTIONS_TABLE_NAME, values,
selection, selectionArgs);
break;
case SOLUTION_ID:
count = db.update(SOLUTIONS_TABLE_NAME, values, _ID +
" = " + uri.getPathSegments().get(1) +
(!TextUtils.isEmpty(selection) ? " AND (" +
selection + ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri );
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
/**
* Get all solution records
*/
case SOLUTIONS:
return "vnd.android.cursor.dir/vnd.example.students";
/**
* Get a particular student
*/
case SOLUTION_ID:
return "vnd.android.cursor.item/vnd.example.students";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
}
当我从SearchDialog执行查询时,我正确地输入并执行query()方法,并且我已经看到查询方法返回的游标中有9个项目....所以它实际上填充了信息。问题是我无法在SearchDialog的最终ListView中看到这些值!
以下是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pioperation.tregliapi.computercomesifa" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainPage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchItem"
android:label="Cerca il problema"
android:theme="@style/AppTheme">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchBackend" />
</activity>
<activity android:name=".SearchBackend"
android:label="Cerca il problema">
<intent-filter>
<action android:name ="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
<provider android:name="pioperation.tregliapi.computercomesifa.SolutionProvider"
android:authorities="pioperation.tregliapi.computercomesifa.SolutionProvider"
>
</provider>
<activity android:name=".VisualizeItem"
android:label="Visualizza le soluzioni presenti"
android:theme="@style/AppTheme">
</activity>
</application>
</manifest>
先谢谢你的帮助!!!
编辑:好的调试我注意到在SolutionProvider里面有这个片段:
//mSearchView.getWindow().getDecorView().post(mStartSpinnerRunnable); // TODO:
try {
cursor = mSearchManager.getSuggestions(mSearchable, query, QUERY_LIMIT);
// trigger fill window so the spinner stays up until the results are copied over and
// closer to being ready
if (cursor != null) {
cursor.getCount();
return cursor;
}
} catch (RuntimeException e) {
Log.w(LOG_TAG, "Search suggestions query threw an exception.", e);
}
// If cursor is null or an exception was thrown, stop the spinner and return null.
// changeCursor doesn't get called if cursor is null
// mSearchView.getWindow().getDecorView().post(mStopSpinnerRunnable); // TODO:
return null;
}
我不知道为什么但是他可以执行getCount()调用,游标不是null,但不知何故他没有返回光标,但是他退出并返回null ......有什么建议吗?
编辑: 好吧我终于找出了什么问题:我做了查询,但我没有在查询中传递Projection Map(HashMap)(实际上我传递的是空的)...所以实际上查询工作正常但是光标没有正确的结构,因此无法显示它...也许有同样问题的人会受益于这个帖子!