简体   繁体   中英

Android SQLITE database join tables in ListView

I have 3 tables in my sqlite android database :

ToolTable
---------
_id
serialNumber
TorqueRange._id

TorqueRangeTable
----------------
_id
min
max
TorqueUnit._id

TorqueUnitTable
---------------
id
name
toNmFactor

I need to use a CursorAdapter to show a ListView of tools but it seems I have multiple choices to expose datas :

  • Use my ContentProvider to return a cursor with all columns I need : _id, serialNumber, minTorque, maxTorque, torqueUnitName
  • Only return ToolTable columns and after in each row view make a new request to get TorqueRange by is id : _id,serialNumber,TorqueRange._id and after in a TorqueRangeTextView get _id,min,max,TorqueUnit._id

What is the best way to accomplish this? Make a join seems easy but after that my ContentProvider returns a lot of columns that I don't need every time. Make a request inside a custom component view respects more the structure of the database but I don't know if it is a good idea to make a lot of requests.

Thanking you for your lights

Ok @zozelfelfo, I choose the first option as you recommanded :

I create my content provider and UriMatcher :

private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final HashMap<String,String> TOOL_PROJECTION_MAP = new HashMap<String,String>();

private static final String TOOL_TABLES = "Tool AS T LEFT JOIN " +
                                          "TorqueRange AS TR ON TR._id = T.TorqueRange_id " +
                                          "TorqueUnit AS TU ON TU._id = TR.TorqueUnit_id";

public static final String KEY_TOOL_ID = "_id";
public static final String KEY_TOOL_SERIAL_NUMBER = "serialNumer";
public static final String KEY_TOOL_MIN_TORQUE = "minTorque";
public static final String KEY_TOOL_MAX_TORQUE = "maxTorque";
public static final String KEY_TOOL_TORQUE_UNIT_NAME = "torqueUnitName";

private static final int TOOLS = 1;
private static final int TOOLS_ID = 2;

static{
  sUriMatcher.add(AUTHORITY,"tools",TOOLS);
  sUriMatcher.add(AUTHORITY,"tools/#",TOOLS_ID);
  TOOL_PROJECTION_MAP.put(KEY_TOOL_ID,"T._id AS T._id");
  TOOL_PROJECTION_MAP.put(KEY_TOOL_SERIAL_NUMBER,"T.serialNumber AS T.serialNumber");
  TOOL_PROJECTION_MAP.put(KEY_TOOL_MIN_TORQUE,"TR.min AS TR.min");
  TOOL_PROJECTION_MAP.put(KEY_TOOL_MAX_TORQUE,"TR.max AS TR.max");
  TOOL_PROJECTION_MAP.put(KEY_TOOL_TORQUE_UNIT_NAME,"TU.name AS TU.name");
}

With this line I can query all Tools with torqueRange data and show them in a CursorAdapter but when I need to insert new rows I can't use insert(...) with "content://"+AUTHORITY+"tools/" because I need also to insert a new TorqueRange row.

Is it a good option to replace my content uri "content://"+AUTHORITY+"tools/" by content://"+AUTHORITY+"toolsWithTorqueRange/" to query in the CursorAdapter, and using "content://"+AUTHORITY+"tools/" for inserting with KEYS : (KEY_TOOL_ID,KEY_TOOL_SERIAL_NUMBER,KEY_TOOL_TORQUE_RANGE_ID)?

Or are there different options?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM