Optimization Suggestions for ListView with Data from Multiple "Tables"

I just faced the following situation. I have an Android application with a script that I think can happen in multiple applications. It's about tagging / labeling / categorization, name it your own. I basically have the following relationships in SQLite DB

 --------                 --------------              ---------
|  Tags  |               |  DeviceTags  |            | Devices |
|--------|               |--------------|            |---------|
| ID     | 1 ------ *    | ID           | * ------ 1 | ID      |
| NAME   |               | TAGS_ID      |            | NAME    |
 --------                | DEVICE_ID    |            | ...     |
                          --------------              ---------

Everything is revealed over the ContentProvider that I wrote. So far so good.

In the user interface part, I have a ListActivity showing all saved Devices (from the Devices table) and to further customize the user interface, I created custom line elements showing a small image in front based on the type of device, etc.

What I would like to do now is also show the related tags in this list for each device. Now my problem arises . For a simple list of devices, I created a custom ResourceCursorAdapter, where I set the relevant 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, in order to find out which tags are associated with my device, I will need to request 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 great, but to be honest, I don't feel good. For some reason it seems strange to me ...

Are there any better solutions on how to solve this problem?

// Edit:

, CommonsWare . CursorAdapter, , , ( , , ).

, - , , , "" :)

+5
2

( ) 100 . , , . CommonsWare, , , s.t. , .

datamodel. , , - , s.t. , ContentProvider, , , :

| ID | NAME             | ... | LABELSSTRING   |
------------------------------------------------
| 1  | "My Device name" | ... | "Work, Friend" |
| 1  | "Some other"     | ... |                |
| 1  | "Yet another"    | ... | "Work"         |

, CursorAdapter bindView (..) , . . SQLite group_concat (X, Y).

0

, . , , , :

  • , , .
  • , "this", .

, , "". do...while() ?: -)

, - ListView. , , " -" . , , , .

, , ContentProvider, - , ( ).

. , , , . , , , , , . , , , - .

+3

All Articles