【问题标题】:make a custom listView in android sorted by date with total在android中制作一个自定义listView,按日期排序,总计
【发布时间】:2016-12-16 12:55:41
【问题描述】:

我想制作一个自定义 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”格式存储,数量是一个整数。 请问你有什么想法吗? 谢谢

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    首先制作一个可以包含Integer的动态数组列表,命名为日期

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

    现在通过将其转换为整数来添加所有值,即,

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

    使用 Total 将值粘贴到标题中

    【讨论】:

    • 感谢您的回答,这看起来是个不错的方法。我尝试实现它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多