Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Android: Problems with creating valid content provider URI
- package com.TBTT;
- // ...
- // some imports
- // ...
- public class TBTTManageBudgetsActivity extends Activity {
- // ...
- // some declarations
- // ...
- @Override
- public void onCreate(Bundle savedInstanceState) {
- // Initialisation
- super .onCreate(savedInstanceState);
- setContentView(R.layout.manage_budgets);
- // ...
- // some stuff concerning other elements
- // ...
- // Fill List View
- // Get the list view
- ListView listView = (ListView) findViewById(R.id.list_box);
- // Get content provider and cursor
- String[] projection = { TBTCPHBudget.KEY_ROWID, TBTCPHBudget.KEY_NAME };
- String[] uiBindFrom = { TBTCPHBudget.KEY_ROWID};
- int[] uiBindTo = { R.id.name };
- TBTCPBdugets ContentProvider = new TBTCPBdugets();
- Cursor cursor = ContentProvider.query(TBTCPBdugets.CONTENT_URI, projection,null, null, null);
- // Let activity manage the cursor
- startManagingCursor(cursor);
- // Get value from content provider
- cursor.moveToFirst();
- ArrayList list = new ArrayList<String>();
- do {
- list.add(cursor.getString(1));
- } while (cursor.moveToNext());
- // Set Values with Adapter
- CursorAdapter adapter = new SimpleCursorAdapter(this.getApplicationContext(), R.layout.list_budgets_item, cursor, uiBindFrom, uiBindTo);
- listView.setAdapter(adapter);
- }
- // ...
- // some other functions
- // ...
- }
- package com.TBTT;
- // ...
- // some imports
- // ...
- public class TBTCPBdugets extends ContentProvider {
- // ...
- // some declarations
- // ...
- public static final String AUTHORITY = "com.TBTT";
- public static final String DATAPATH = "TBTCPHBudget";
- public static final Uri CONTENT_URI = Uri.parse("content://"+ AUTHORITY+"/" + DATAPATH);
- private static final UriMatcher sUriMatcher;
- private static class DatabaseHelper extends SQLiteOpenHelper {
- // ...
- // some other functions
- // ...
- }
- private DatabaseHelper dbHelper;
- // ...
- // some other functions
- // ...
- @Override
- public boolean onCreate() {
- dbHelper = new DatabaseHelper(getContext());
- return true;
- }
- @Override
- public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
- SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
- switch (sUriMatcher.match(uri)) {
- case BUDGETS:
- qb.setTables(BUDGETS_TABLE_NAME);
- qb.setProjectionMap(notesProjectionMap);
- break;
- default:
- Log.d(TAG, "Unknown URI " + uri+" with URIMatcher Result "+sUriMatcher.match(uri));
- throw new IllegalArgumentException("Unknown URI " + uri);
- }
- SQLiteDatabase db = dbHelper.getReadableDatabase();
- Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
- c.setNotificationUri(getContext().getContentResolver(), uri);
- return c;
- }
- static {
- sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- sUriMatcher.addURI(AUTHORITY, BUDGETS_TABLE_NAME, BUDGETS);
- // ...
- // some declarations
- // ...
- }
- }
Add Comment
Please, Sign In to add comment