当我尝试阅读表格时出错。我有一个包含几个表的数据库,但我的uri似乎不匹配。
我的内容提供商:
public class CardContentProvider extends ContentProvider {
private StockCard stock;
public static String authority = "com.example.jean.cartememoire.CardContentProvider";
private static String path ="Cartes_table";
public static final String _ID = "_id";
public static final String THEME = "THEME";
public static final String QUESTION = "QUESTION";
public static final String REPONSE = "REPONSE";
public static final String DIFFICULTE = "DIFFICULTE"; //# = un chiffre
public static final String STOCK_TABLE = "Cartes_table";
public static final String STOCK_FACILE = "";
public static final String STOCK_NORMAL = "Cartes_tableCartes_table_facile_normal";
public static final String STOCK_DIFFICILE = "Cartes_table_difficile";
public static final String STOCK_EXTREME = "Cartes_table_extreme";
public static final String STOCK_IMPOSSIBLE = "Cartes_table_impossible";
private static final int ID_STOCK_TABLE = 1;
public static final int ID_FACILE = 2;
public static final int ID_NORMAL = 3;
public static final int ID_DIFFICILE = 4;
public static final int ID_EXTREME = 5;
public static final int ID_IMPOSSIBLE = 6;
private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
matcher.addURI(authority, STOCK_TABLE, ID_STOCK_TABLE);
matcher.addURI(authority, STOCK_TABLE+"/*", ID_STOCK_TABLE);
matcher.addURI(authority, STOCK_FACILE+"/*", ID_FACILE);
matcher.addURI(authority, STOCK_NORMAL+"/*", ID_NORMAL);
matcher.addURI(authority, STOCK_DIFFICILE+"/*", ID_DIFFICILE);
matcher.addURI(authority, STOCK_EXTREME+"/*", ID_EXTREME);
matcher.addURI(authority, STOCK_IMPOSSIBLE+"/*", ID_IMPOSSIBLE);
}
@Override
public boolean onCreate() {
stock = StockCard.getInstance(getContext());
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = stock.getReadableDatabase();
int code = matcher.match(uri);
Cursor cursor;
switch (code)
{
case ID_STOCK_TABLE:
Log.d("Uri provider = "+code+" ", uri.toString()+" ");
cursor = db.query(STOCK_TABLE, projection, selection,
selectionArgs, null, null, sortOrder);
break;
case ID_FACILE:
cursor = db.query(STOCK_FACILE, projection, selection,
selectionArgs, null, null, sortOrder);
break;
case ID_NORMAL:
cursor = db.query(STOCK_NORMAL, projection, selection,
selectionArgs, null, null, sortOrder);
break;
case ID_DIFFICILE:
cursor = db.query(STOCK_DIFFICILE, projection, selection,
selectionArgs, null, null, sortOrder);
break;
case ID_EXTREME:
cursor = db.query(STOCK_EXTREME, projection, selection,
selectionArgs, null, null, sortOrder);
break;
case ID_IMPOSSIBLE:
cursor = db.query(STOCK_IMPOSSIBLE, projection, selection,
selectionArgs, null, null, sortOrder);
break;
default:
//long u = ContentUris.parseId(uri);
Log.d("Uri provider = "+code+" ", uri.toString()+" ");
throw new UnsupportedOperationException("Pas encore implémenté");
}
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = stock.getWritableDatabase();
int code = matcher.match(uri);
long id;
Uri.Builder builder = new Uri.Builder();
switch(code)
{
case ID_STOCK_TABLE:
id = db.insert(STOCK_TABLE, null, values);
builder.appendPath(STOCK_TABLE);
break;
case ID_FACILE:
id = db.insert(STOCK_FACILE, null, values);
builder.appendPath(STOCK_FACILE);
break;
case ID_NORMAL:
id = db.insert(STOCK_NORMAL, null, values);
builder.appendPath(STOCK_NORMAL);
break;
case ID_DIFFICILE:
id = db.insert(STOCK_DIFFICILE, null, values);
builder.appendPath(STOCK_DIFFICILE);
break;
case ID_EXTREME:
id = db.insert(STOCK_EXTREME, null, values);
builder.appendPath(STOCK_EXTREME);
break;
case ID_IMPOSSIBLE:
id = db.insert(STOCK_IMPOSSIBLE, null, values);
builder.appendPath(STOCK_IMPOSSIBLE);
break;
default:
throw new UnsupportedOperationException("Pas encore implémenté");
}
builder.authority(authority);
builder = ContentUris.appendId(builder, id);
return builder.build();
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = stock.getWritableDatabase();
int code = matcher.match(uri);
int i;
long id;
switch(code)
{
case ID_STOCK_TABLE:
id = ContentUris.parseId(uri);
i = db.delete(STOCK_TABLE, "_id=" + id, null);
break;
case ID_FACILE:
id = ContentUris.parseId(uri);
i = db.delete(STOCK_FACILE, "_id=" + id, null);
break;
case ID_NORMAL:
id = ContentUris.parseId(uri);
i = db.delete(STOCK_NORMAL, "_id=" + id, null);
break;
case ID_DIFFICILE:
id = ContentUris.parseId(uri);
i = db.delete(STOCK_DIFFICILE, "_id=" + id, null);
break;
case ID_EXTREME:
id = ContentUris.parseId(uri);
i = db.delete(STOCK_EXTREME, "_id=" + id, null);
break;
case ID_IMPOSSIBLE:
id = ContentUris.parseId(uri);
i = db.delete(STOCK_IMPOSSIBLE, "_id=" + id, null);
break;
default:
throw new UnsupportedOperationException("Pas encore implémenté");
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
包含我的Loader的My Fragment类,在我的函数数据库视图中,我将行放在List视图中:
public class ViewDeck extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String authority = "com.example.jean.cartememoire.CardContentProvider";
public String[] from;
public final int[] to = {R.id.idList, R.id.themeList, R.id.questionList, R.id.reponseList, R.id.difficultList};
StockCard stock;
ViewGroup container;
ListView listView;
SimpleCursorAdapter adapter;
Cursor cursor;
private String focus;
private ArrayList<String> data = new ArrayList<String>();
private String table_name;
public ViewDeck()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup c,
Bundle savedInstanceState) {
container = c;
View view = inflater.inflate(R.layout.fragment_view_card_editor, container, false);
stock = StockCard.getInstance(container.getContext());
from = new String[]{stock._ID,
stock.THEME,
stock.QUESTION,
stock.REPONSE,
stock.DIFFICULTE};
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
Bundle f = getArguments();
focus = getStockName(f.getInt("f"));
databaseView(view);
return view;
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri.Builder builder = new Uri.Builder();
Uri uri = builder.scheme("content").authority(authority)
.appendPath("Cartes_table_facile").build();
System.out.println("URI : "+uri.toString());
//Log.d("Uri provider = "+code+" ", uri.toString()+" ");
return new CursorLoader(container.getContext(), uri, from,
null, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
public String getStockName(int d)
{
switch(d+1)
{
case CardContentProvider.ID_FACILE:
return CardContentProvider.STOCK_FACILE;
case CardContentProvider.ID_NORMAL:
return CardContentProvider.STOCK_NORMAL;
case CardContentProvider.ID_DIFFICILE:
return CardContentProvider.STOCK_DIFFICILE;
case CardContentProvider.ID_EXTREME:
return CardContentProvider.STOCK_EXTREME;
case CardContentProvider.ID_IMPOSSIBLE:
return CardContentProvider.STOCK_IMPOSSIBLE;
default:
throw new UnsupportedOperationException("Difficulté inconnue");
}
}
public void databaseView(View view)
{
listView = (ListView) view.findViewById(R.id.listView);
System.out.println("DANS DATABASEVIEW");
adapter = new SimpleCursorAdapter(container.getContext(), R.layout.card_stock, null, from, to,0);
listView.setAdapter(adapter);
if(cursor != null)
cursor.close();
LoaderManager manager = getLoaderManager();
manager.initLoader(0, null, this);
if(cursor != null)
cursor.close();
System.out.println("CA GAZ");
}
}
编辑:我的错误:
01-04 17:45:45.507 28655-28691/? E/AndroidRuntime: FATAL EXCEPTION: ModernAsyncTask #1
Process: com.example.jean.cartememoire, PID: 28655
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:153)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.UnsupportedOperationException: Pas encore implémenté
at com.example.jean.cartememoire.CardContentProvider.query(CardContentProvider.java:101)
at android.content.ContentProvider.query(ContentProvider.java:1017)
at android.content.ContentProvider$Transport.query(ContentProvider.java:238)
at android.content.ContentResolver.query(ContentResolver.java:491)
at android.support.v4.content.ContentResolverCompatJellybean.query(ContentResolverCompatJellybean.java:29)
at android.support.v4.content.ContentResolverCompat$ContentResolverCompatImplJB.query(ContentResolverCompat.java:57)
at android.support.v4.content.ContentResolverCompat.query(ContentResolverCompat.java:125)
at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:59)
at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:37)
at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:296)
at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:54)
at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:42)
at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:133)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)