2016-12-16 72 views
0

我想爲每天的一些數據和一個節標題製作一個自定義listView。 在此部分標題希望將日期和每天的總數。 我很難計算每個部分標題的總數。 我的數據來源是一個數據庫。 這個樣子的: enter image description here在android中創建自定義listView按日期排序按合計

我的適配器:

public class CursorSectionAdapter extends CursorAdapter { 
public static final String TAG = "debug_adapter"; 

public static final String BABY_BOTTLE_TAG = "baby_bottle"; 
int i = 0; 
private LayoutInflater cursorInflater; 

private LinkedHashMap<String, Integer> mSections; 
private LinkedHashMap<String, Integer> mSectionsDate; 


private ArrayList mSectionIndex; 

BaseAdapter baseAdapter; 

private Cursor mCursor; 
private static final int STATE_UNKNOWN = 0; 
private static final int TYPE_SEPARATOR = 1; 
private static final int TYPE_ITEM = 2; 


public CursorSectionAdapter(Context context, Cursor c, boolean autoRequery) { 
    super(context, c, autoRequery); 
    this.mSections = new LinkedHashMap<String, Integer>(); 
    this.mSectionsDate = new LinkedHashMap<String, Integer>(); 
    this.mSectionIndex = new ArrayList(); 

    mCursor = c; 
    findSection(); 
    cursorInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 

@Override 
public void changeCursor(Cursor cursor) { 
    mCursor = cursor; 
    // int i = cursor.getCount(); 
    findSection(); 
    super.changeCursor(cursor); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    return cursorInflater.inflate(R.layout.baby_bottle_item_layout, parent, false); 
} 


@Override 
public void bindView(View view, Context context, Cursor cursor) { 

    i++; 


    TextView dateTimeTv = (TextView) view.findViewById(R.id.BabyBottleItemDateTimeTextView); 
    String dateTime[] = cursor.getString(cursor.getColumnIndex(BiberonDataBaseContract.BabyBottleTable.COLUMN_DATETIME)).split(" "); 
    dateTimeTv.setText(dateTime[1]); 
    //Todo test dateTime not null 
    TextView quantityTv = (TextView) view.findViewById(R.id.BabyBottleItemQuantityTextView); 
    String quantity = cursor.getString(cursor.getColumnIndex(BiberonDataBaseContract.BabyBottleTable.COLUMN_QUANTITY)); 
    quantityTv.setText(quantity + "ml"); 

    TextView otherTv = (TextView) view.findViewById(R.id.BabyBottleItemOtherTextView); 
    TextView separator = (TextView) view.findViewById(R.id.separatorTextView); 



    int cursorPos = mCursor.getPosition(); 
    switch (getItemViewType(cursorPos)) { 
     case TYPE_ITEM: 
      Log.d(TAG, "TYPE_ITEM"); 
      separator.setVisibility(View.GONE); 
      break; 
     case TYPE_SEPARATOR: 

      String result = Utils.DateFromDataBaseToLocal(dateTime[0]); //Get date to european format 
      //String result = String.format("%s-%s-%s",day,month,year); 
      separator.setText(result + " Total : "); // How get total.... 
      separator.setVisibility(View.VISIBLE); 
      Log.d(TAG, "TYPE_SEPARATOR"); 
      break; 
    } 

     int vitamine = cursor.getInt(cursor.getColumnIndex(BiberonDataBaseContract.BabyBottleTable.COLUMN_VITAMIN)); 
     int iron = cursor.getInt(cursor.getColumnIndex(BiberonDataBaseContract.BabyBottleTable.COLUMN_IRON)); 
     int saddle = cursor.getInt(cursor.getColumnIndex(BiberonDataBaseContract.BabyBottleTable.COLUMN_SADDLE)); 
     int urin = cursor.getInt(cursor.getColumnIndex(BiberonDataBaseContract.BabyBottleTable.COLUMN_URIN)); 
     boolean bVit = (vitamine != 0); 
     boolean bIron = (iron != 0); 
     boolean bSaddle = (saddle != 0); 
     boolean bUrin = (urin != 0); 


     String other = ""; 
     StringBuilder sb = new StringBuilder(other); 

     if (bVit) sb.append("Vitamine-"); 
     if (bIron) sb.append("Fer-"); 
     if (bUrin) sb.append("Urine-"); 
     if (bSaddle) sb.append("Selle-"); 
     int lio = sb.lastIndexOf("-"); 
     int l = (sb.length() - 1); 
     if (lio == l && lio > 0) sb.deleteCharAt(lio); 

     otherTv.setText(sb.toString()); 

} 


@Override 
public int getItemViewType(int position) { 
    return mSectionsDate.containsValue(position) ? TYPE_SEPARATOR : TYPE_ITEM; 
} 

private void findSection() { 
    if (mCursor != null) { 
     int nSection = 0; 
     int i = 0; 
     int total = 0; 
     mSections.clear(); 
     Log.d(BABY_BOTTLE_TAG, "find section : count : " + mCursor.getCount()); 

     int index = mCursor.getColumnIndex(BiberonDataBaseContract.BabyBottleTable.COLUMN_DATETIME); 
     mCursor.moveToFirst(); 
     while (mCursor.isAfterLast() == false) { 
      String sectionName = mCursor.getString(index).split(" ")[0]; 
      String[] sectionName2 = mCursor.getString(index).split(" "); 

      if (!mSections.containsKey(sectionName)) { 
       mSections.put(sectionName, i + nSection); 
       mSectionsDate.put(sectionName2[0], i); 
       mSectionIndex.add(i); 

       nSection++; 
      } 
      i++; 


      // mCursor.getString(2); 
      mCursor.moveToNext(); 
     } 
     Log.d(TAG, "Found " + mSections.toString() + " section"); 
     /// ; 
    } 
} 

}

這裏是我的活動我的裝載機要求:

@Override 
public Loader onCreateLoader(int id, Bundle args) { 
    String[] PROJECTION = new String[] { 
      BiberonDataBaseContract.BabyBottleTable._ID, // 0 
      BiberonDataBaseContract.BabyBottleTable.COLUMN_QUANTITY, // 1 
      BiberonDataBaseContract.BabyBottleTable.COLUMN_DATETIME, //2 
      BiberonDataBaseContract.BabyBottleTable.COLUMN_URIN,//3 
      BiberonDataBaseContract.BabyBottleTable.COLUMN_IRON,//4 
      BiberonDataBaseContract.BabyBottleTable.COLUMN_SADDLE,//5 
      BiberonDataBaseContract.BabyBottleTable.COLUMN_VITAMIN//6 

    }; 

    // Filter results WHERE "_id" = 'babyId' 
    String selection = BiberonDataBaseContract.BabyBottleTable.COLUMN_BABY_ID + " = ?"; 
    String[] selectionArgs = { String.valueOf(babyId) }; 


    Uri testUri =BiberonDataBaseContract.BabyBottleTable.CONTENT_URI; 
    return new CursorLoader(this.getBaseContext(),testUri,PROJECTION,selection,selectionArgs,null); 
} 

在我的數據庫COLUMN_DATETIME存儲在「YYYY-MM-DD HH:MM:SS」的格式和數量是一個整數。 你有什麼想法嗎? 感謝

回答

0

首先製作一個動態數組列表可以contatin整數,並將它命名爲日期

List<Integer> date_20161216=new ArrayList<Integer>(); 

現在,所有值添加到它通過將其轉換爲intger,即

//suppose you want to add 225ml and 255ml,then 
String value=255ml; 
date_20161216.add(Integer.parseInt(value.value.indexOf("m"))); 

然後當你想添加它們

int total=0; 
for(int i=0;i<date_20161216;i++){ 
    total=total+date_20161216.get(i)); 
} 

使用總數來粘貼hea值der

+0

感謝您的回答,這看起來是一個很好的方法。我嘗試實現它。 – dav