我刚刚遇到了以下情况。我有一个Android应用程序,我猜可能会发生在多个应用程序中。它是关于标记/标签/分类的,你想怎么叫就怎么叫。我在SQLite数据库中基本上有以下关系
-------- -------------- ---------
| Tags | | DeviceTags | | Devices |
|--------| |--------------| |---------|
| ID | 1 ------ * | ID | * ------ 1 | ID |
| NAME | | TAGS_ID | | NAME |
-------- | DEVICE_ID | | ... |
-------------- ---------
一切都是通过我写的内容提供者公开的。到目前为止一切都很好。
我的问题来了
. 对于简单的设备列表,我创建了一个定制的ResourceCursorAdapter,在其中我在bindView方法中设置了相应的信息
@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
...
}
如您所见,为了能够知道我的设备关联了哪种类型的标记,我需要查询设备标记。所以我做了:
@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;
}
我测试了这个,它实际上工作得很好,但说实话,我觉得做这个不太好。我觉得有点奇怪。。。
//编辑:
在Commonware的反馈之后,这里有一点澄清。我对在CursorAdapter中对DB进行第二次查询感到奇怪,基本上这会导致每行一次查询,我担心这会严重影响我的性能(我仍然需要在具有大量数据的真实设备上进行测试,看看这会有多大影响)。
我的问题