=== about me ===
I’m new to android development and barely able to “speak” Java. Nevertheless I’m experienced in PHP5 and C++ C++/CLI (.net >2.0) so I hope I’ll be able to create this little app, that is in my mind.
But after a few days of experimenting I’m in a kind of pitfall now, and hope you can help me.
=== about the project ===
I’ve got an listview (nested within other layout) and try to populate it with data from a sqlite. I managed to get the thing up and running with a simple “arrayAdapter”, but researching how I could update it if I insert new elemnts in the database with a popup I figured out that I need to implement my own content provider.
=== about the problem ===
I browsed a few tutorials and did so, but now I have problems calling it because of the URI. I don’t understand how it should be created and so I can’t create a valid one.
Here is the problem in code:
TBTTManageBudgetsActivity.java
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
// ...
}
TBTCPBdugets.java
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
// ...
}
}
When I run this and open the TBTTManageBudgetsActivity the program will crash. The debug output from the TBTCPBdugets.java says: “Unknown URI content://com.TBTT/TBTCPBdugets with URIMatcher Result -1”.
Can anybody tell me please how to create a VALID URI for this?
Your UriMatcher don’t know about your CONTENT_URI. There are DATAPATH in CONTENT_URI, but BUDGETS_TABLE_NAME in matcher.