数据库文件正常工作时RecyclerView并没有打印所有数据

问题描述 投票:0回答:1

我正在为我的研究制作一个简单的任务管理器应用程序,但我的回收视图存在问题,没有显示我的所有数据。我添加了 3 个不同的表,但它只在我的回收视图上显示第一个表。希望大家能帮忙看看到底是什么问题。非常感谢你的帮助

这是我的数据库,运行良好 1 测试 123 测试是否有效 17/5/2024 2 测试 456 测试 第二次 17/5/2024 3 再次测试 测试检查数据库 31/5/2024

MainActivity.java

package com.example.a41p;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;


import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    FloatingActionButton add_button;
    MyDatabaseHelper myDB;
    ArrayList<String> task_title, task_description, task_duedate;
    CustomAdapter customAdapter;

    @SuppressLint("CutPasteId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize views
        recyclerView = findViewById(R.id.recyclerview);
        add_button = findViewById(R.id.floatingActionButton);
        Toolbar toolbar = findViewById(R.id.toolbar);

        toolbar.setNavigationOnClickListener(view -> {
            // Navigate back to MainActivity
            Intent intent = new Intent(MainActivity.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish(); // Finish current activity
        });

        // Set onClickListener for the FloatingActionButton
        add_button.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, AddActivity.class);
            addActivityResultLauncher.launch(intent);
        });

        myDB = new MyDatabaseHelper(MainActivity.this);
        task_title = new ArrayList<>();
        task_description = new ArrayList<>();
        task_duedate = new ArrayList<>();

        // Store data from the database into the array
        storeDataInArrays(false);

        // Set up the RecyclerView with the CustomAdapter
        customAdapter = new CustomAdapter(MainActivity.this, this, task_title, task_description, task_duedate);
        recyclerView.setAdapter(customAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    }


    //I think is the problem but seems to look fine
    private final ActivityResultLauncher<Intent> addActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    // Append new data to the RecyclerView
                    storeDataInArrays(true);
                    customAdapter.notifyDataSetChanged();
                }
            }
    );

    void storeDataInArrays(boolean append) {
        Cursor cursor = myDB.readAllData();
        if (cursor.getCount() == 0) {
            Toast.makeText(this, "No data.", Toast.LENGTH_SHORT).show();
        } else {
            if (!append) {
                task_title.clear();
                task_description.clear();
                task_duedate.clear();
            }
            while (cursor.moveToNext()) {
                task_title.add(cursor.getString(1));
                task_description.add(cursor.getString(2));
                task_duedate.add(cursor.getString(3));
            }
        }
    }
}

我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:title="Task List"
            app:titleTextColor="@android:color/white"
            app:layout_scrollFlags="scroll|enterAlways" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@id/appBarLayout"
        app:layout_constraintBottom_toTopOf="@id/floatingActionButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="96dp" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:srcCompat="@drawable/ic_add"
        android:layout_gravity="bottom|end"
        android:clickable="true"
        android:focusable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:ignore="ContentDescription,SpeakableTextPresentCheck" />

</androidx.constraintlayout.widget.ConstraintLayout>

这就是它的样子

我认为这部分是问题所在,因为我确实向chatgpt询问过它,但它说这看起来不错,我的目标只是在每次添加新任务时打印出卡片视图。

private final ActivityResultLauncher<Intent> addActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    // Append new data to the RecyclerView
                    storeDataInArrays(true);
                    customAdapter.notifyDataSetChanged();
                }
            }
    );

这是我的 CustomApdapter,它与 my_row xml 一起使用

package com.example.a41p;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

    private final Context context;
    private final Activity activity;
    private final ArrayList<String> task_title;
    private final ArrayList<String> task_description;
    private final ArrayList<String> task_duedate;

    public CustomAdapter(Activity activity, Context context, ArrayList<String> task_title, ArrayList<String> task_description, ArrayList<String> task_duedate) {
        this.activity = activity;
        this.context = context;
        this.task_title = task_title;
        this.task_description = task_description;
        this.task_duedate = task_duedate;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.my_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        holder.task_title_txt.setText(task_title.get(position));
        holder.task_description_txt.setText(task_description.get(position));
        holder.task_duedate_txt.setText(task_duedate.get(position));

        holder.mainLayout.setOnClickListener(view -> {
            int adapterPosition = holder.getAdapterPosition();
            if (adapterPosition != RecyclerView.NO_POSITION) {
                Intent intent = new Intent(context, UpdateActivity.class);
                intent.putExtra("title", String.valueOf(task_title.get(adapterPosition)));
                intent.putExtra("description", String.valueOf(task_description.get(adapterPosition)));
                intent.putExtra("due date", String.valueOf(task_duedate.get(adapterPosition)));
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return task_title.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView task_title_txt, task_description_txt, task_duedate_txt;
        LinearLayout mainLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            task_title_txt = itemView.findViewById(R.id.task_title_txt);
            task_description_txt = itemView.findViewById(R.id.task_description_txt);
            task_duedate_txt = itemView.findViewById(R.id.task_duedate_txt);
            mainLayout = itemView.findViewById(R.id.mainLayout);
        }
    }

}

my_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="12dp"
    android:id="@+id/mainLayout">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="172dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="171dp">

            <TextView
                android:id="@+id/task_title_txt"
                android:layout_width="354dp"
                android:layout_height="38dp"
                android:layout_marginStart="13dp"
                android:layout_marginEnd="12dp"
                android:text="Title"
                android:textColor="@android:color/black"
                android:textSize="30sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                tools:ignore="HardcodedText,MissingConstraints"
                tools:layout_editor_absoluteY="7dp" />

            <TextView
                android:id="@+id/task_description_txt"
                android:layout_width="356dp"
                android:layout_height="67dp"
                android:layout_marginStart="13dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="12dp"
                android:text="Description"
                android:textSize="20sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/task_title_txt"
                tools:ignore="HardcodedText,MissingConstraints" />

            <TextView
                android:id="@+id/task_duedate_txt"
                android:layout_width="141dp"
                android:layout_height="29dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:text="Due Date"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/task_description_txt"
                tools:ignore="MissingConstraints" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

</LinearLayout>

java android xml
1个回答
0
投票

也许可以尝试直接在数据添加部分下方刷新
并更改 my_row xml 父布局的高度以扭曲内容,如果您不希望它采用孔屏(如果您想保留它)

您甚至可以获取列表的正确位置,然后使用 customAdapter.notifyItemInserted(position);

void storeDataInArrays(boolean append) {
    Cursor cursor = myDB.readAllData();
    if (cursor.getCount() == 0) {
        Toast.makeText(this, "No data.", Toast.LENGTH_SHORT).show();
    } else {
        if (!append) {
            task_title.clear();
            task_description.clear();
            task_duedate.clear();
        }
        while (cursor.moveToNext()) {
            task_title.add(cursor.getString(1));
            task_description.add(cursor.getString(2));
            task_duedate.add(cursor.getString(3));
            customAdapter.notifyDataSetChanged();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.