开发者

How to do that more efficiently? (ListAdapter)

I have a List and each entry has a dynamic count of attributes which should show up. Each attribute in a row has a name and a value. This implementation works, but how could I do this in a more efficient way? By now I need to build up the whole list/maps before what is very time-consuming...:

    // get category names
    Map<Long, String> categoriesNames = new HashMap<Long, String>();
    // ...

    // create log-list of maps for SimpleAdapter
    List<Map<String, String>> logsMap = new ArrayList<Map<String, String>>();
    // getAll() is just: db.query(true, TABLE_NAME, null, null, null, null, null, Columns.TIMESTAMP + " desc", null);
    Cursor logs = logDao.getAll();
    try {
        int idIndex = logs.getColumnIndex("_id");
        int timestampIndex = logs.getColumnIndex("timestamp");
        int categoryIdIndex = logs.getColumnIndex("category_id");
        int noteIndex = logs.getColumnIndex("note");
        while (logs.moveToNext()) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("category", categoriesNames.get(logs.getLong(categoryIdIndex)));
            map.put("note", getString(R.string.note) + ": " + logs.getString(noteIndex));
            map.put("timestamp", MyDateUtils.formatDateTime(logs.getLong(timestampIndex)));
            map.put("attributes", attributeLogDao.getAttributesAndValuesByLogId(logs.getLong(idIndex)));
            logsMap.add(map);
        }
    } finally {
        logs.close();
    }

    // Mapping
    String[] from = new String[] { "category", "note", "timestamp", "attributes" };
    int[] to = new int[] {
            R.id.log_list_row_category, R.id.log_list_row_note,
            R.id.log_list_row_datetime, R.id.log_list_row_attributes };

    // ListView Adapter
    SimpleAdapter simpleAdapter = new SimpleAdapter(this,
            logsMap, R.layout.log_list_row, from, to);
    ListView lv = (ListView) this.findViewById(R.id.listView);
    lv.setAdapter(simpleAdapter);

By now I'm building a list to use simpleAdapter. I don't know how I could use a CursorAdapter or so, because of this line:

map.put("attributes", attributeLogDao.getAttributesAndValuesByLogId(logs.getLong(idIndex)));

(almost last line in the while-loop)

What this call does is:

public String getAttributesAndValuesByLogId(long logId) {
    String attributesString = "";
    Cursor c = db.getSQLiteDatabase().rawQuery("SELECT al." + Columns.VALUE + " AS value, a." + AttributeDao.Columns.NAME + " AS a_name" +
            " FROM " + TABLE_NAME + " AS al, " + AttributeDao.TABLE_NAME + " AS a" +
            " WHERE al." + Columns.LOG_ID + " = " + logId +
            " AND a." + AttributeDao.Columns.ID + " = al." + Columns.ATTRIBUTE_ID +
            " ORDER BY value DESC", 开发者_运维知识库null);
    if (c != null && c.moveToFirst()) {
        do {
            attributesString += c.getString(1) + " (" + c.getLong(0) + ")\n";
        } while (c.moveToNext());
        c.close();
    }
    return attributesString;
}

Thanks!


To point you in the write direction:

  • Use a SimpleCursorAdapter, leaving most of your code intact.
  • Implement SimpleCursorAdapter.ViewBinder which needs one method, setViewValue.
  • Your implementation of setViewValue will be almost identical to the current body of your while loop.
  • Link the two using SimpleCursorAdapter#setViewBinder.

The purpose of the view binder is to give you a chance to customize how your columns affect the rows they are bound to. The implementation of setViewValue will look something like:

if (columnIndex == categoryIdIndex) {
    ... populate view with category name ...
    return true;
}
if (columnIndex == timestampIndex) {
    ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜