使用从SQLite概括的数据填充RecyclerView / ListView

时间:2019-02-25 15:33:23

标签: java android listview android-recyclerview

我想用RecyclerView ListView的数据填充我的SQLite / ItemDB ...而且我也想归纳所有数据,例如:

我的数据(在数据库中,标签和金额列):

  • 交通运输-100美元
  • 交通运输-500美元
  • 食物-300美元
  • 账单-1000美元
  • 账单-1600美元

RecyclerView / ListView中将仅显示3个项目:运输,食品和账单。将运输的2值与账单一并添加/组合。 >

如何通过RecyclerView填充ListView / SQLite并执行我想做的验证/方法?

图表片段的XML文件:在这里,我将listView放在pichart下方。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f6f6f6"
    tools:context=".Charts">

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/pastelPink"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView18"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp"
        android:fontFamily="@font/montserrat"
        android:text="Distribution Chart"
        android:textColor="#fefefe"
        android:textSize="25dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/view"
        app:layout_constraintEnd_toEndOf="@+id/view"
        app:layout_constraintStart_toStartOf="@+id/view"
        app:layout_constraintTop_toTopOf="@+id/view" />

    <com.razerdp.widget.animatedpieview.AnimatedPieView
        android:id="@+id/animatedPieChart"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/textView6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="152dp"
        android:fontFamily="@font/montserrat"
        android:text="Data tablehere (Expenses okung ano man andito sa baba)"
        android:textColor="#242a2c"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

图表片段的Java代码:将把列表视图放在其中的Java代码

package com.example.admin.test2;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import com.razerdp.widget.animatedpieview.AnimatedPieView;
import com.razerdp.widget.animatedpieview.AnimatedPieViewConfig;
import com.razerdp.widget.animatedpieview.data.SimplePieInfo;

public class Charts extends Fragment {

    AnimatedPieView mAnimatedPieView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_charts, container, false);

        mAnimatedPieView = view.findViewById(R.id.animatedPieChart);
        drawPie();

        return view;
    }

    public void drawPie() {

        AnimatedPieViewConfig config = new AnimatedPieViewConfig();
        config.startAngle(-90)// Starting angle offset
                .addData(new SimplePieInfo(30, Color.parseColor("#77dd77"), "Sample Data to"))//Data (bean that implements the IPieInfo interface)
                .addData(new SimplePieInfo(18.0f, Color.parseColor("#ff6961"), "Sample Data ulet")).drawText(true)
                .duration(3000);// draw pie animation duration
        config.floatShadowRadius(18f);
        config.floatUpDuration(500);
        config.interpolator(new DecelerateInterpolator(4f));
// The following two sentences can be replace directly 'mAnimatedPieView.start (config); '
        mAnimatedPieView.applyConfig(config);
        mAnimatedPieView.start();
    }

}

ITEM数据库助手

package com.example.admin.test2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.admin.test2.ItemContract.*;


public class ItemDBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "itemlist.db";
    public static final int DATABASE_VERSION = 4;

    public ItemDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        final String SQL_CREATE_ITEMLIST_TABLE = "CREATE TABLE " + ItemEntry.TABLE_NAME + " (" +
                ItemEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                ItemEntry.COLUMN_LABEL + " TEXT NOT NULL, " +
                ItemEntry.COLUMN_DETAIL + " TEXT, " +
                ItemEntry.COLUMN_AMOUNT + " INTEGER NOT NULL, " +
                ItemEntry.COLUMN_DATE + " TEXT " +
                ");";

        db.execSQL(SQL_CREATE_ITEMLIST_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + ItemEntry.TABLE_NAME);
        onCreate(db);
    }
}

项目合同

package com.example.admin.test2;

import android.provider.BaseColumns;

public class ItemContract {

    private ItemContract() {}

    public static final class ItemEntry implements BaseColumns
    {
        public static final String TABLE_NAME = "items";
        public static final String COLUMN_LABEL = "label";
        public static final String COLUMN_AMOUNT = "amount";
        public static final String COLUMN_DETAIL = "detail";
        public static final String COLUMN_DATE = "date";

    }
}

1 个答案:

答案 0 :(得分:0)

以下是一些高级构想,可以帮助您入门。

  1. 将下面的SQL转换为SQLite选择方法以获取所需的数据。
  2. 用于填充RecylclerView / ListView的实现适配器

    SELECT标签,SUM(数量) 来自项目 GROUP BY标签

我还建议您使用Room库,该库大大简化了SQLite的使用。 Room Persistence Library