我想从“ REALM DATABASE”中删除数据,但是它给我错误?

时间:2019-12-26 08:15:09

标签: java android android-recyclerview realm

我希望你一切都好。

我想从领域数据库中以及从cardview recyclerview中单击按钮上的特定记录删除时,当我单击(ADAPTER CLASS)中的删除按钮时,它给我错误,我不知道如何解决。我希望当我单击删除按钮时,数据也已从领域数据库和recyclerview(cardview)中删除,但是我没有成功。

请帮助我,我是领域数据库的新手,希望您能给我更好的答复。 预先感谢。

enter image description here RealmHelper类别:

    package com.deitel.realmrecyclerviewspinnerpriority.Database;
import android.content.Context;
import android.view.Display;
import android.widget.Toast;

import com.deitel.realmrecyclerviewspinnerpriority.Modelclass.ModelClass;
import java.util.ArrayList;
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmQuery;
import io.realm.RealmResults;
public class RelmHelper {
    Realm realm;
        Context context;
    public RelmHelper(Realm realm, Context context) {
        this.realm = realm;
        this.context=context;
    }

    public void savedata(final ModelClass modelClass) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                ModelClass md = realm.copyToRealm(modelClass);
            }
        });
    }

    public ArrayList<ModelClass> retrievedata() {
        ArrayList<ModelClass> modelclassdata = new ArrayList<>();
        RealmResults<ModelClass> modelClass = realm.where(ModelClass.class).findAll();
        for (ModelClass MD : modelClass) {
            modelclassdata.add(MD);
        }
        return modelclassdata;
    }
public void deletedata(final int id)
{
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            ModelClass modelClass=realm.where(ModelClass.class).equalTo("id",id).findFirst();
            if (modelClass!=null)
            {
                modelClass.deleteFromRealm();

            }
            realm.close();
        }
    });
}
}

适配器类:

    package com.deitel.realmrecyclerviewspinnerpriority.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

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

import com.deitel.realmrecyclerviewspinnerpriority.Database.RelmHelper;
import com.deitel.realmrecyclerviewspinnerpriority.Modelclass.ModelClass;
import com.deitel.realmrecyclerviewspinnerpriority.R;

import java.util.ArrayList;

import io.realm.Realm;

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.Viewholder> {
    Realm realm;
    RelmHelper relmHelper;

    ArrayList<ModelClass> modelClassList;
    Context context;

    public AdapterClass(@NonNull  Context context, ArrayList<ModelClass> modelClassList) {
        this.modelClassList = modelClassList;
        this.context = context;
        relmHelper=new RelmHelper(realm, context);
    }

    @NonNull
    @Override
    public AdapterClass.Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_cardview_layout, viewGroup, false);
        Viewholder holder = new Viewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterClass.Viewholder holder, final int position) {
        holder.textView_name.setText("Name :" + modelClassList.get(position).getName());
        holder.textView_details.setText("Details : " + modelClassList.get(position).getDetails());
        holder.textView_priority.setText("Priority : " + modelClassList.get(position).getPriority());
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                relmHelper.deletedata(modelClassList.get(position).getId());
                modelClassList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, modelClassList.size());
                notifyDataSetChanged();
            }
        });
    }
    @Override
    public int getItemCount() {
        return modelClassList.size();
    }
    public class Viewholder extends RecyclerView.ViewHolder {
        TextView textView_name;
        TextView textView_details;
        TextView textView_priority;
        ImageButton btn_delete;

        public Viewholder(@NonNull View view) {
            super(view);
            textView_name = view.findViewById(R.id.textview_name);
            textView_details = view.findViewById(R.id.textview_details);
            textView_priority = view.findViewById(R.id.textview_priotity);
            btn_delete = view.findViewById(R.id.btn_delete);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您尚未在realm内部初始化AdapterClass来初始化RelmHelper。请尝试以下:

realm = Realm.getInstance(context); // Also can use Realm.getDefaultInstance();
relmHelper=new RelmHelper(realm, context);