2016-02-17 32 views
1

我其實有兩個問題。我做這個用戶輸入日誌的Android應用程序。在佈局中,我有按地點分組的列表,我需要顯示該地點所有日誌的計數。 這是我對分組地方獲取同一地點的數據並在TextView中顯示它的數量

public ArrayList<Logs> getAllLogsGroupedByPlace() { 
     ArrayList<Logs> logByPlaceList = new ArrayList<Logs>(); 
     String selectQuery = "SELECT * FROM " + TABLE_LOGS + " GROUP BY " + KEY_PLACE; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     if (cursor.moveToFirst()) { 
      do { 
       Logs log = new Logs(); 
       log.setId(cursor.getLong(0)); 
       log.setCreatedAt(cursor.getString(1)); 
       log.setPlace(cursor.getString(2)); 
       logByPlaceList.add(log); 
      }while (cursor.moveToNext()); 
     } 
     return logByPlaceList; 
    } 

代碼,這是我通過地方計數日誌代碼

public int getLogsCountPlace() { 
     String countQuery = "SELECT * FROM " + TABLE_LOGS + " WHERE " + KEY_PLACE + "=" + KEY_PLACE; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     cursor.close(); 

     return cursor.getCount(); 
    } 

所以我的問題是如何正確計算這些日誌由地方進行分組,然後在顯示他們我列出textview?

回答

0

你應該改變你的getLogsCountPlace方法。

public Map<String,Integer> getLogsCountPlace() { 
     String countQuery = "SELECT COUNT(*), " + KEY_PLACE + " FROM " + TABLE_LOGS + " GROUP BY " + KEY_PLACE; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     //// The cursor will return 2 columns, the first column will have the count of each place and the second column will have the place name. You could store the counts in a Map, having the place as the key and the count as the value. 
     cursor.close(); 
    } 
+0

好吧,我明白了。但是,如何在活動中調用它並將它放入TextView? – RubyDigger19

+0

只需遍歷遊標並追加列值,然後將其設置爲TextView。 –

+0

沒有明白。你能給我一個代碼樣本嗎? – RubyDigger19

相關問題