我想从“ REALM DATABASE”中删除数据,但不删除当前的ID数据吗?

时间:2019-12-30 05:59:50

标签: java android android-recyclerview realm

希望您一切都好。 当我按下按钮单击要删除的数据时,我想从ID基础上的android studio中的领域数据库中删除数据,但我要删除的数据(id 0)保持不变,但我不希望删除。在cardview中单击按钮,应该删除所选数据而不是id 0数据。我是REALM DATABASE的新手,请帮帮我。 希望您能给我更好的答复。 我正在等待您的回复。 上帝保佑你们。 问候。

MainActivity.Class

    package com.deitel.realmrecyclerviewspinnerpriority;

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

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.deitel.realmrecyclerviewspinnerpriority.Adapter.AdapterClass;
import com.deitel.realmrecyclerviewspinnerpriority.Database.RelmHelper;
import com.deitel.realmrecyclerviewspinnerpriority.Interface.InterfaceClass;
import com.deitel.realmrecyclerviewspinnerpriority.Modelclass.ModelClass;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import io.realm.Realm;

public class MainActivity extends AppCompatActivity implements InterfaceClass {
    EditText text_name;
    EditText text_details;
    ImageButton btn_ascending, btn_descending;
    RecyclerView rv;
    Spinner spinner;
    Button btn_save, btn_view;
    ArrayList<ModelClass> itemslist;
    AdapterClass addapter;
    RelmHelper helper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text_name = findViewById(R.id.text_name);
        text_details = findViewById(R.id.text_details);
        btn_save = findViewById(R.id.btn_show);
        btn_view = findViewById(R.id.btn_view);
        btn_save = findViewById(R.id.btn_show);
        rv = findViewById(R.id.recyclerview);
        Realm.init(this);
        final Realm realm = Realm.getDefaultInstance();
        helper = new RelmHelper(realm, this);
        itemslist = helper.retrievedata();
        btn_ascending = findViewById(R.id.btn_ascending);
        btn_descending = findViewById(R.id.btn_descneding);
        rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        addapter = new AdapterClass(this, itemslist, realm, this);
///*addapter=new AdapterClass(this, itemslist, realm, new InterfaceClass() {
//            @Override
//            public void deletedata(int position) {
//                *//*perform any action for multiple adapter on one method*//*
//            }
//        });*/
        rv.setAdapter(addapter);
        spinner = findViewById(R.id.spinner);
        final ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(4);
        arrayList.add(5);
        arrayList.add(6);
        arrayList.add(7);
        arrayList.add(8);
        arrayList.add(9);
        arrayList.add(10);
        arrayList.add(11);
        arrayList.add(12);
        arrayList.add(13);
        final ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, arrayList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        btn_ascending.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Ascending_order();
            }
        });
        btn_descending.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Descending_order();
            }
        });
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = text_name.getText().toString();
                String details = text_details.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(getApplicationContext(), "Enter Name", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(details)) {
                    Toast.makeText(getApplicationContext(), "Enter Details", Toast.LENGTH_SHORT).show();
                    return;
                }
//                ModelClass md = new ModelClass(name, details, priority);
                ModelClass md = new ModelClass();
                md.setName(text_name.getText().toString());
                md.setDetails(text_details.getText().toString());
                md.setPriority(Integer.parseInt(spinner.getSelectedItem().toString()));
                /*save data*/
                helper.savedata(md);
                Toast.makeText(MainActivity.this, "Data Successfully Added", Toast.LENGTH_SHORT).show();
                text_name.setText("");
                text_details.setText("");

            }
        });

    }

    public void Ascending_order() {
        Collections.sort(itemslist, new Comparator<ModelClass>() {
            @Override
            public int compare(ModelClass o1, ModelClass o2) {
                Integer p1 = o1.getPriority();
                Integer p2 = o2.getPriority();
                return p1.compareTo(p2);
            }
        });
        Toast.makeText(this, "Ascending Order", Toast.LENGTH_SHORT).show();
        addapter.notifyDataSetChanged();
    }

    public void Descending_order() {
        Collections.sort(itemslist, new Comparator<ModelClass>() {
            @Override
            public int compare(ModelClass o1, ModelClass o2) {
                Integer p1 = o1.getPriority();
                Integer p2 = o2.getPriority();
                return p2.compareTo(p1);
            }

        });
        Toast.makeText(this, "Descending Order", Toast.LENGTH_SHORT).show();
        addapter.notifyDataSetChanged();
    }


    @Override
    public void deletedata(int position) {
        helper.deletedata(position);
    }

}

AdapterClass

    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.Interface.InterfaceClass;
import com.deitel.realmrecyclerviewspinnerpriority.MainActivity;
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> itemslist;
    private InterfaceClass interfaceClass;
    Context context;

    public AdapterClass(@NonNull Context context, ArrayList<ModelClass> itemslist, Realm realm, InterfaceClass interfaceClass) {
        this.context = context;
        this.itemslist = itemslist;
        this.interfaceClass = interfaceClass;
        this.realm = realm;
        realm = Realm.getDefaultInstance();
        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 :" + itemslist.get(position).getName());
        holder.textView_details.setText("Details : " + itemslist.get(position).getDetails());
        holder.textView_priority.setText("Priority : " + itemslist.get(position).getPriority());
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                interfaceClass.deletedata(itemslist.get(position).getId());
                itemslist.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, itemslist.size());
                    notifyDataSetChanged();
//                if (context instanceof MainActivity)
//                {
//                    ((MainActivity)context).deletedata(itemslist.get(position).getId());
//                    itemslist.remove(position);
//                    notifyItemRemoved(position);
//                    notifyItemRangeChanged(position, itemslist.size());
//                    notifyDataSetChanged();
//                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return itemslist.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);
        }
    }
}

ModelClass

    package com.deitel.realmrecyclerviewspinnerpriority.Modelclass;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

public class ModelClass extends RealmObject {
    private int id;
    private String name;
    private String details;
    private int priority;


//         public  ModelClass()
//         {
//
//         }
//
//    public ModelClass(String name, String details, int priority) {
//        this.name=name;
//        this.details=details;
//        this.priority=priority;
//    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }
}

RealHelper类

    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.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 result = realm.where(ModelClass.class).equalTo("id", id).findFirst();
                result.deleteFromRealm();

            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                Toast.makeText(context, "Data Deleted", Toast.LENGTH_SHORT).show();
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

我的申请

    package com.deitel.realmrecyclerviewspinnerpriority;

import android.app.Application;

import io.realm.Realm;
import io.realm.RealmConfiguration;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder().schemaVersion(1).deleteRealmIfMigrationNeeded().build();
        Realm.setDefaultConfiguration(config);
    }
}

InterfaceClass

    package com.deitel.realmrecyclerviewspinnerpriority.Interface;

public interface InterfaceClass {
     void deletedata(int position);
}



0 个答案:

没有答案