Hi,
I just encountered the following situation. I have an Android app with a scenario which I guess may happen in multiple apps. It's about tagging/labeling/categorizing, call it as you want. I basically have the following relations in the SQLite DB
-------- -------------- ---------
| Tags | | DeviceTags | | Devices |
|--------| |--------------| |---------|
| ID | 1 ------ * | ID | * ------ 1 | ID |
| NAME | | TAGS_ID | | NAME |
-------- | DEVICE_ID | | ... |
-------------- ---------
Everything is exposed over a ContentProvider I've written. So far everything is fine.
On the UI part, I have an ListActivity showing all of the stored Devices (from the Devices table) and for customizing the UI a bit further, I created custom row items showing a small image in front based on the device type etc.
What I'd like to achieve now is to also show the associated tags on that list for each device. Now here comes my problem. For the simple device list I created a custom ResourceCursorAdapter where I set the according information in the bindView method
@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
final int objectId = cursor.getInt(cursor.getColumnIndex(Devices._ID));
TextView deviceName = (TextView) view.findViewById(R.id.deviceName);
deviceName.setText(...); //set it from the cursor
...
TextView associatedTagsView = (TextView)...;
associatedTagsView.setText(...); //<<<???? This would need a call to a different table
...
}
As you can see, for being able to know what kind of tags my device has associated, I'd need to query DeviceTags. So I did:
@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
...
TextView associatedTagsView = (TextView)view.findViewById(R.id.deviceTags);
String tagsString = retrieveTagsString(view.getContext().getContentResolver(), objectId);
...
}
private String retrieveTagsString(ContentResolver contentResolver, int objectId) {
Cursor tagForDeviceCursor = contentResolver.query(DroidSenseProviderMetaData.TABLE_JOINS.TAG_DEVICETAG,...);
if(tagForDeviceCursor != null && tagForDeviceCursor.moveToFirst()){
StringBuffer result = new StringBuffer();
boolean isFirst = true;
do{
if(!isFirst)
result.append(", ");
else
isFirst = false;
result.append(retrieve name from cursor column...);
}while(tagForDeviceCursor.moveToNext());
return result.toString();
}
return null;
}
I tested this and it actually works just fine, but to be honest I don't feel well doing this. Somehow seems weird to me...
Are there any better solutions on how to solve this??
//Edit:
After CommonsWare's feedback here a bit of a clarification. I'm weird about doing the second query to the DB inside the CursorAdapter, basically this would result in one query per row and I fear this will heavily impact my performance (I've still to test it on a real device with a substantial amount of data to see how much this impacts).
My question therefore is on whether there are some strategies on how to avoid this given my data model or whether I have to basically "live" with that :)