Android 如何获取指定月份的数据并将其除以天数

Android 如何获取指定月份的数据并将其除以天数,android,Android,我想和你核实一下, 我想为我的申请做一个总结。 这样就可以给我一个月的平均价格 这意味着我有一组记录,我已经按月过滤了。 如果month=Jan,我想得到所有的Jan数据并除以该月的天数。 马上 我只能拿出几个月。 我想核实一下, 如果我想做“想得到所有一月的数据并除以一个月的天数”,我该怎么做? 有人能给我提个建议吗 我是android新手,正在努力学习 protected void onCreate(Bundle savedInstanceState) { super.onC

我想和你核实一下, 我想为我的申请做一个总结。 这样就可以给我一个月的平均价格

这意味着我有一组记录,我已经按月过滤了。 如果month=Jan,我想得到所有的Jan数据并除以该月的天数。 马上 我只能拿出几个月。 我想核实一下, 如果我想做“想得到所有一月的数据并除以一个月的天数”,我该怎么做? 有人能给我提个建议吗

我是android新手,正在努力学习

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.summary);

        monthDate = (EditText)findViewById(R.id.month);
        avgPrice = (TextView)findViewById(R.id.showavfPriceTV);
        exFuel = (TextView)findViewById(R.id.showexFuelTV);
        avgFC = (TextView)findViewById(R.id.showavgFCTV);
        doneButton = (Button)findViewById(R.id.doneBTN);
        exitButton = (Button)findViewById(R.id.exitBTN);



            // First we need to make contact with the database we have created using
            // the DbHelper class
            AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);

            // Then we need to get a readable database
            SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();

            // We need a a guy to read the database query. Cursor interface will do
            // it for us
            // (String table, String[] columns, String selection, String[]
            // selectionArgs, String groupBy, String having, String orderBy)
            Cursor cursor = sqliteDatabase.query(
                    AndroidOpenDbHelper.TABLE_NAME_LOG, null, null, null, null,
                    null, null);
            // Above given query, read all the columns and fields of the table

            startManagingCursor(cursor);

            // Cursor object read all the fields. So we make sure to check it will
            // not miss any by looping through a while loop
            while (cursor.moveToNext()) {
                // In one loop, cursor read one undergraduate all details
                // Assume, we also need to see all the details of each and every
                // undergraduate
                // What we have to do is in each loop, read all the values, pass
                // them to the POJO class
                // and create a ArrayList of undergraduates
                String id = cursor.getString(cursor
                        .getColumnIndex(AndroidOpenDbHelper.KEY_ROWID));

                final String date = cursor.getString(cursor
                        .getColumnIndex(AndroidOpenDbHelper.KEY_DATE));
                String price = cursor.getString(cursor
                        .getColumnIndex(AndroidOpenDbHelper.KEY_PRICE));
                String pump = cursor.getString(cursor
                        .getColumnIndex(AndroidOpenDbHelper.KEY_FUEL));
                String cost = cursor.getString(cursor
                        .getColumnIndex(AndroidOpenDbHelper.KEY_COST));
                String odm = cursor.getString(cursor
                        .getColumnIndex(AndroidOpenDbHelper.KEY_ODM));
                String preodm = cursor.getString(cursor
                        .getColumnIndex(AndroidOpenDbHelper.KEY_PREODM));
                String fcon = cursor.getString(cursor
                        .getColumnIndex(AndroidOpenDbHelper.KEY_CON));



            // If you don't close the database, you will get an error
            sqliteDatabase.close();
                Log.i("FuelLog", "dataBundle " + date);

        monthDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               // showDialog(DATE_DIALOG_ID);
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
            }
        });


doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {




                if (monthDate.getText().toString().equals(date.subSequence(3,9).toString()))

{

                    Log.d("MONTHDATE","date : "+ monthDate.getText().toString());
                    Log.d("BBUNDLEDATE","date : "+ (date.subSequence(3,9).toString()));


                     Intent intent=new Intent(getApplicationContext(),homeActivity.class);
                     startActivity(intent);


                }
                else 
                {
                     Intent intent=new Intent(getApplicationContext(),about.class);
                     startActivity(intent);
                    Log.d("ELSEMONTHDATE","date : "+ monthDate.getText().toString());
                    Log.d("ELSEBUNDLEDATE","date : "+ (date.subSequence(3,9).toString()));
                }






            }
        });
}

    }

假设你有一个月的月度数据总和

Cursor cur = myDB.rawQuery
    ("SELECT SUM(Price) FROM Prices WHERE Month = 1", null);
if(cur.moveToFirst())
{
    return cur.getInt(0);
}
更好的是,您可以通过一个查询获得给定年份的月份和每个月的总价格:

String sql = "SELECT Month, SUM(Price) FROM Prices WHERE Year = ? GROUP BY Month ORDER BY Month";
将单个月数据除以:

Calendar cal = new GregorianCalendar(2014, Calendar.JANUARY, 1); // Specify an year and a month basing on your datum
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
请记住:(来自SQLite参考站点)

总和(X)
总数(X) sum()和total()聚合函数返回组中所有非空值的总和。如果没有非空的输入行,则sum()返回NULL,但total()返回0.0。NULL通常对于无行之和不是一个有用的结果,但SQL标准要求它,并且大多数其他SQL数据库引擎都以这种方式实现sum(),因此SQLite也以同样的方式实现它,以便兼容。非标准的total()函数是用SQL语言解决此设计问题的一种方便方法

The result of total() is always a floating point value. The result of sum() is an integer value if all non-NULL inputs are integers. If any input to sum() is neither an integer or a NULL then sum() returns a floating point value which might be an approximation to the true sum.

Sum() will throw an "integer overflow" exception if all inputs are integers or NULL and an integer overflow occurs at any point during the computation. Total() never throws an integer overflow. 
另请注意


在没有指定价格的几个月内设置虚拟值(0.0)总是一个好主意

我不明白,你能进一步解释吗?我只是对如何获取数据感到困惑。例如,我有一组记录,2条记录是1月,3条记录是2月。如果我单击Summary(2月),我想将2月的价格记录加在一起,然后除以天数。我该如何做?我该如何添加价格?每个月是否有1个或更多数据?如果你有更多,请按我的答案加起来。如果只有1个值,就足够了。然后将该值除以天数(返回指定年份指定月份的最大天数)求平均值。对不起,此代码(“从月份=1的价格中选择总和(价格)”,null);他们在一起吗?