简体   繁体   English

使用从SQLite导出的数据填充RecyclerView / ListView

[英]Populate RecyclerView/ListView with data generalized from SQLite

I want to populate my RecyclerView / ListView with data from SQLite ItemDB ... And also I want to generalize all the data, for example: 我想用来自SQLite ItemDB数据填充我的RecyclerView / ListView ...而且我想归纳所有数据,例如:

My data(in database, the label and amount column): 我的数据(在数据库中,标签和金额列):

  • Transportation - 100dollars 交通费-100美元
  • Transportation - 500dollars 交通-500美元
  • Food - 300dollars 食物-300美元
  • Bills - 1000dollars 账单-1000USD
  • Bills - 1600dollars 账单-1600USD

What will appear in the RecyclerView / ListView is only 3 items: Transportation, Food, and Bills.. the Transportation's 2 value is added/combined as one as well as Bills.. RecyclerView / ListView中将仅显示3个项目:运输,食品和账单。运输的2值与账单一起被添加/合并。

How do I populate my RecyclerView / ListView through SQLite and do this validation/method I want to do?? 如何通过SQLite填充RecyclerView / ListView并执行我想做的验证/方法?

XML FILE FOR CHARTS FRAGMENT: here I will put the listView in below the pichart. 图表片段的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 CODE FOR CHARTS FRAGMENT: the java code where I will put the listview 图表片段的Java代码:我将把listview放到的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 DATABASE HELPER 项目数据库帮助

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

ITEM CONTRACT 项目合同

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";

    }
}

Here are high level ideas to get you started. 以下是一些高级构想,可帮助您入门。

  1. Convert below SQL to SQLite select method to get the desired data. 将下面的SQL转换为SQLite select方法以获得所需的数据。
  2. Implement adapter to fill the RecylclerView/ListView 实现适配器以填充RecylclerView / ListView

    SELECT label, SUM(amount) FROM items GROUP BY label SELECT标签,SUM(amount)来自项目GROUP BY标签

I also suggest you utilize the Room library, which greatly simplifies SQLite utilization. 我还建议您使用Room库,它可以大大简化SQLite的使用。 Room Persistence Library 房间持久性库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM