Guest User

Untitled

a guest
Aug 16th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. Android: Problems with creating valid content provider URI
  2. package com.TBTT;
  3.  
  4. // ...
  5. // some imports
  6. // ...
  7.  
  8. public class TBTTManageBudgetsActivity extends Activity {
  9. // ...
  10. // some declarations
  11. // ...
  12.  
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. // Initialisation
  16. super .onCreate(savedInstanceState);
  17. setContentView(R.layout.manage_budgets);
  18.  
  19. // ...
  20. // some stuff concerning other elements
  21. // ...
  22.  
  23. // Fill List View
  24. // Get the list view
  25. ListView listView = (ListView) findViewById(R.id.list_box);
  26. // Get content provider and cursor
  27. String[] projection = { TBTCPHBudget.KEY_ROWID, TBTCPHBudget.KEY_NAME };
  28. String[] uiBindFrom = { TBTCPHBudget.KEY_ROWID};
  29. int[] uiBindTo = { R.id.name };
  30. TBTCPBdugets ContentProvider = new TBTCPBdugets();
  31. Cursor cursor = ContentProvider.query(TBTCPBdugets.CONTENT_URI, projection,null, null, null);
  32. // Let activity manage the cursor
  33. startManagingCursor(cursor);
  34. // Get value from content provider
  35. cursor.moveToFirst();
  36. ArrayList list = new ArrayList<String>();
  37. do {
  38. list.add(cursor.getString(1));
  39. } while (cursor.moveToNext());
  40. // Set Values with Adapter
  41. CursorAdapter adapter = new SimpleCursorAdapter(this.getApplicationContext(), R.layout.list_budgets_item, cursor, uiBindFrom, uiBindTo);
  42. listView.setAdapter(adapter);
  43. }
  44.  
  45. // ...
  46. // some other functions
  47. // ...
  48. }
  49.  
  50. package com.TBTT;
  51. // ...
  52. // some imports
  53. // ...
  54.  
  55. public class TBTCPBdugets extends ContentProvider {
  56. // ...
  57. // some declarations
  58. // ...
  59. public static final String AUTHORITY = "com.TBTT";
  60. public static final String DATAPATH = "TBTCPHBudget";
  61. public static final Uri CONTENT_URI = Uri.parse("content://"+ AUTHORITY+"/" + DATAPATH);
  62. private static final UriMatcher sUriMatcher;
  63.  
  64. private static class DatabaseHelper extends SQLiteOpenHelper {
  65. // ...
  66. // some other functions
  67. // ...
  68. }
  69.  
  70. private DatabaseHelper dbHelper;
  71.  
  72. // ...
  73. // some other functions
  74. // ...
  75.  
  76. @Override
  77. public boolean onCreate() {
  78. dbHelper = new DatabaseHelper(getContext());
  79. return true;
  80. }
  81.  
  82. @Override
  83. public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  84. SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
  85. switch (sUriMatcher.match(uri)) {
  86. case BUDGETS:
  87. qb.setTables(BUDGETS_TABLE_NAME);
  88. qb.setProjectionMap(notesProjectionMap);
  89. break;
  90. default:
  91. Log.d(TAG, "Unknown URI " + uri+" with URIMatcher Result "+sUriMatcher.match(uri));
  92. throw new IllegalArgumentException("Unknown URI " + uri);
  93. }
  94.  
  95. SQLiteDatabase db = dbHelper.getReadableDatabase();
  96. Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
  97.  
  98. c.setNotificationUri(getContext().getContentResolver(), uri);
  99. return c;
  100. }
  101.  
  102. static {
  103. sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  104. sUriMatcher.addURI(AUTHORITY, BUDGETS_TABLE_NAME, BUDGETS);
  105.  
  106. // ...
  107. // some declarations
  108. // ...
  109. }
  110. }
Add Comment
Please, Sign In to add comment