2011-03-31 75 views
0

我有一個列表,每個條目都有應顯示的屬性的動態計數。一行中的每個屬性都有一個名稱和一個值。這個實現有效,但我怎麼能以更有效的方式來做到這一點?現在我需要的是非常耗時之前建立起來的整個列表/地圖...:如何更有效地做到這一點? (ListAdapter)

// 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); 

現在我建立一個列表使用simpleAdapter。我不知道我怎麼會用一個CursorAdapter左右,因爲這行:

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

(在while循環幾乎最後一行)

什麼這個調用所做的是:

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; 
} 

謝謝!

回答

1

要指出你在寫方向:

  • 使用SimpleCursorAdapter,使大部分代碼不變。
  • 實施SimpleCursorAdapter.ViewBinder需要一種方法,setViewValue
  • 您的setViewValue的實現將與您的while循環的當前正文幾乎相同。
  • 使用SimpleCursorAdapter#setViewBinder鏈接兩個。

視圖聯編程序的目的是給你一個機會來定製你的列如何影響它們綁定的行。 setViewValue的實現如下所示:

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

謝謝!!這工作。現在唯一的問題是,它自己的列表滾動是急躁的,因爲在每個「setViewValue」屬性LogDao.getAttributesAndValuesByLogId(logs.getLong(idIndex))需要被調用......但無論如何,這是更好的然後一個很長的加載在開始... – Stuck 2011-04-01 12:34:11

+0

你可以通過連接獲取屬性嗎? (我假設你的getAttributes ...方法正在進行額外的數據庫查詢。) – 2011-04-01 14:47:55

相關問題