新文本框中来自数据库的值总和

时间:2018-12-05 15:59:01

标签: android database sum

在将所有数据库值的总和显示到一个文本框中时出现问题。

这是来自activity_main.xml的代码

<TextView
        android:id="@+id/ukupno"
        android:layout_width="172dp"
        android:layout_height="94dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="212dp"
        android:layout_marginBottom="60dp"
        android:textAppearance="?android:textAppearanceLarge"
        android:textStyle="italic"
        tool:hint="Ukupno" />

这是MainActivity.java中的代码

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = {
            ArtiklContract.ArtiklEntry._ID,
            ArtiklContract.ArtiklEntry.COLUMN_IME_PROIZVODA,
            ArtiklContract.ArtiklEntry.COLUMN_CIJENA,
            ArtiklContract.ArtiklEntry.COLUMN_TRGOVINA,
            ArtiklContract.ArtiklEntry.COLUMN_KOLCINA};

    return new CursorLoader( this, ArtiklContract.ArtiklEntry.CONTENT_URI, projection, null, null, null );
}

这是ArtiklCursorAdapter中的代码

@Override
    public void bindView(final View view, final Context context, Cursor cursor) {
        TextView nameView = view.findViewById( R.id.name_product );
        TextView priceView = view.findViewById( R.id.price_product );
        TextView quantityView = view.findViewById( R.id.quantity_product );
        TextView storeView = view.findViewById( R.id.product_store );
        TextView totalView = view.findViewById( R.id.total_price );


        int nameColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_IME_PROIZVODA );
        int priceColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_CIJENA );
        int quantityColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_KOLCINA );
        int storeColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_TRGOVINA );
        int totalColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_UKUPNO );
        final int productIdColumnIndex = cursor.getInt( cursor.getColumnIndex( ArtiklContract.ArtiklEntry._ID ) );

        String nameCurrent = cursor.getString( nameColumn );
        double priceCurrent = cursor.getDouble( priceColumn );
        final int quantityCurrent = cursor.getInt( quantityColumn );
        String storeCurrent = cursor.getString( storeColumn );
        double totalCurrent = priceCurrent * quantityCurrent;

        nameView.setText( nameCurrent );
        priceView.setText( String.valueOf( priceCurrent ) );
        totalView.setText( new DecimalFormat( "##.##" ).format( totalCurrent ) );
        storeView.setText( storeCurrent );
        quantityView.setText( String.valueOf( quantityCurrent ) );
    }

现在,我想对每一行的所有double totalCurrent求和,并在“ ukupno”文本框中显示总和。

TY寻求帮助!

1 个答案:

答案 0 :(得分:0)

您仅从游标读取一行数据。您应该在光标上循环

粗略地说:

@Override
public void bindView(final View view, final Context context, Cursor cursor) {
    TextView nameView = view.findViewById( R.id.name_product );
    TextView priceView = view.findViewById( R.id.price_product );
    TextView quantityView = view.findViewById( R.id.quantity_product );
    TextView storeView = view.findViewById( R.id.product_store );
    TextView totalView = view.findViewById( R.id.total_price );

    // check null and move the cursor to the first position
    if(cursor != null && !cursor.moveToFirst()){
         return; // fail early if there is no data or null
    }


    String nameCurrent = null;
    String storeCurrent = null; 
    double totalCurrent = 0; 

    // loop the contents
    while(!cursor.isAfterLast()){

        //.. start adding up your values here

        int nameColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_IME_PROIZVODA );
        int priceColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_CIJENA );
        int quantityColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_KOLCINA );
        int storeColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_TRGOVINA );
        int totalColumn = cursor.getColumnIndex( ArtiklContract.ArtiklEntry.COLUMN_UKUPNO );
         final int productIdColumnIndex = cursor.getInt( cursor.getColumnIndex( ArtiklContract.ArtiklEntry._ID ) );

        // Assuming you want only one name and store, I would check null
        nameCurrent = cursor.getString( nameColumn );
        storeCurrent = cursor.getString( storeColumn );

        int priceCurrent = cursor.getDouble( priceColumn );
        int quantityCurrent = cursor.getInt( quantityColumn );        
        totalCurrent += priceCurrent * quantityCurrent;

        // move the cursor
        cursor.moveToNext();
    }

    // these I am not sure apply so i commented them out
    //priceView.setText( String.valueOf( priceCurrent ) );
    //quantityView.setText( String.valueOf( quantityCurrent ) );

    nameView.setText( nameCurrent );
    storeView.setText( storeCurrent );
    totalView.setText( new DecimalFormat( "##.##" ).format( totalCurrent ) );
}

这确实很粗糙,但是这个概念成立。您想遍历光标窗口并获取所有条目以计算总数。我不确定您打算如何显示这些内容,但实际上我会构建某种类型的数组,然后使用适配器将其传递给RecyclerView或ListView。

祝你好运,编码愉快!