有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用来自SQLite的通用数据填充RecyclerView/ListView

我想用来自SQLite{}的数据填充我的RecyclerView/ListView。。。我还想概括所有数据,例如:

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

  • 交通费——100美元
  • 交通费——500美元
  • 食物——300美元
  • 账单——1000美元
  • 账单-1600美元

RecyclerView/ListView中只会出现三个项目:交通、食品和账单。。运输的2个价值与账单一起增加/合并

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

图表片段的XML文件:这里我将把listView放在pichart下面

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

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

    <TextView
        安卓:id="@+id/textView18"
        安卓:layout_width="wrap_content"
        安卓:layout_height="0dp"
        安卓:layout_marginTop="3dp"
        安卓:layout_marginBottom="3dp"
        安卓:fontFamily="@font/montserrat"
        安卓:text="Distribution Chart"
        安卓:textColor="#fefefe"
        安卓:textSize="25dp"
        安卓: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
        安卓:id="@+id/animatedPieChart"
        安卓:layout_width="300dp"
        安卓:layout_height="300dp"
        安卓:layout_marginStart="8dp"
        安卓:layout_marginTop="8dp"
        安卓:layout_marginEnd="8dp"
        安卓: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
        安卓:id="@+id/textView6"
        安卓:layout_width="0dp"
        安卓:layout_height="wrap_content"
        安卓:layout_marginStart="8dp"
        安卓:layout_marginEnd="8dp"
        安卓:layout_marginBottom="152dp"
        安卓:fontFamily="@font/montserrat"
        安卓:text="Data tablehere (Expenses okung ano man andito sa baba)"
        安卓:textColor="#242a2c"
        安卓:textSize="20dp"
        安卓:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

</安卓.support.constraint.ConstraintLayout>

图表片段的JAVA代码:我将在其中放置listview的JAVA代码

package com.example.admin.test2;

import 安卓.graphics.Color;
import 安卓.os.Bundle;
import 安卓.support.v4.app.Fragment;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.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();
    }

}

项目数据库助手

package com.example.admin.test2;

import 安卓.content.Context;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.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 安卓.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) 个答案

  1. # 1 楼答案

    这里有一些高层次的想法可以让你开始

    1. 将下面的SQL转换为SQLite select方法以获得所需的数据
    2. 实现适配器以填充RecylclerView/ListView

      选择标签、金额(金额) 从项目 按标签分组

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