提问者:小点点

将新项目添加到房间数据库后更新RecyclerView视图


我正在android上制作计算器应用程序。我有两个页面,计算器页面和历史页面。该应用程序将数学运算,结果和操作时间保存在房间数据库中。我希望应用程序在按“equals”按钮后在历史页面中显示这三个实体。

现在我的计算器应用程序在关闭它后显示实体。如何修复/更改此问题?我已经以多种方式尝试过通知fyDataSetChanged(),但它不起作用。有时它会停止在历史页面中显示任何数据。

如果你需要更多的信息或者你不理解我,请随时询问。我的英语不是最好的。: D

适配器:

package com.example.simplecalculator;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;


public class CalculatorListAdapter extends 
RecyclerView.Adapter<CalculatorListAdapter.ViewHolder> {
public CalculatorListAdapter() {
}

public List<CalculationEntities> localDataset;


public CalculatorListAdapter(List<CalculationEntities> dataset) {
    localDataset = dataset;

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = 
 LayoutInflater.from(parent.getContext()).inflate(R.layout.calculation_history_item, 
 parent, false);
    return new ViewHolder(view);
}


@Override
public void onBindViewHolder(@NonNull CalculatorListAdapter.ViewHolder holder, int 
position) {
    holder.itemView.setTag(position);

    holder.tvCalculation.setText(localDataset.get(position).calculation);
    holder.tvResult.setText(localDataset.get(position).result);
    holder.tvTimeStamp.setText(localDataset.get(position).timeStamp);
}


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

public static class ViewHolder extends RecyclerView.ViewHolder {
    public final TextView tvResult;
    public final TextView tvCalculation;
    public final TextView tvTimeStamp;


    public ViewHolder(View itemView) {
        super(itemView);

        tvCalculation = (TextView) itemView.findViewById(R.id.tvOperation);
        tvResult = (TextView) itemView.findViewById(R.id.tvResult);
        tvTimeStamp = (TextView) itemView.findViewById(R.id.tvTimeStamp);
    }
}

}

历史片段:

package com.example.simplecalculator;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.RoomDatabase;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Arrays;
import java.util.List;


public class History extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_history, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    RecyclerView rvHistory = view.findViewById(R.id.rvCalculationHistory);
    List<CalculationEntities> dataset = Arrays.asList(Calculator.database.CalculatorDao().getAllCalculations());

    CalculatorListAdapter calculatorListAdapter = new CalculatorListAdapter(dataset);
    rvHistory.setAdapter(calculatorListAdapter);
    rvHistory.setLayoutManager(new LinearLayoutManager(getContext()));

    super.onViewCreated(view, savedInstanceState);
}

}

DAO:

package com.example.simplecalculator;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

@Dao
public interface CalculatorDao {

    @Query("SELECT * FROM CalculationEntities WHERE calculation LIKE :name LIMIT 1")
    CalculationEntities findByName(String name);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(CalculationEntities... calculations);

    @Query("SELECT * FROM CalculationEntities")
    CalculationEntities[] getAllCalculations();

    @Query("DELETE FROM CalculationEntities")
    void deleteAll();

}

实体:

package com.example.simplecalculator;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity
public class CalculationEntities {

@PrimaryKey(autoGenerate = true)
public int CalculationID;

@ColumnInfo(name="calculation")
public String calculation;

@ColumnInfo(name="result")
public String result;

@ColumnInfo(name="timeStamp")
public String timeStamp;

}

package com.example.simplecalculator;

import androidx.room.Database;
import androidx.room.RoomDatabase;

@Database(entities = {CalculationEntities.class}, version = 1)
public abstract class CalculatorDatabase extends RoomDatabase {
    public abstract CalculatorDao CalculatorDao();

}

更新:仍然不工作:/

更新:将数据插入数据库后localdataset会增加,但回收视图不会更新…


共2个答案

匿名用户

最好使用LiveData进行实时查询更新。当您使用Room时,LiveData有一个很好的扩展。例如。

匿名用户

历史课:

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    CalculatorListAdapter calculatorListAdapter = new CalculatorListAdapter();
    calculatorListAdapter.notifyDataSetChanged();

    RecyclerView rvHistory = view.findViewById(R.id.rvCalculationHistory);
    rvHistory.setAdapter(calculatorListAdapter);
    rvHistory.setLayoutManager(new LinearLayoutManager(getContext()));

    super.onViewCreated(view, savedInstanceState);
}

适配器类:

public CalculatorListAdapter() {
    ReloadData();

}


public void ReloadData(){
    localDataset = Calculator.database.CalculatorDao().getAllCalculations();
    this.notifyDataSetChanged();
}

相等按钮:

        else if (id == R.id.btnEqual) {

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM HH:mm");
        Equals equals = new Equals(output.getText().toString(), simpleDateFormat.format(calendar.getTime()));

        equals.isEqualTo();
        InitiateCalculation(equals.operation,equals.result,equals.operationTime);

        CalculatorListAdapter calculatorListAdapter = new CalculatorListAdapter();
        calculatorListAdapter.ReloadData();
        calculatorListAdapter.notifyDataSetChanged();

        output.setText(null);
        output.setText(equals.result);
    }