将数据存储在“ REALM DATABASE”中并在recyclerview中显示

时间:2019-12-25 13:37:02

标签: java android android-recyclerview realm

我想将我的数据存储在领域数据库中,然后检索并将其显示在recyclerview中。从我的角度来看,我所有的代码都是正确的,但是它给我一个有关空指针异常的错误。尽管做了我的努力,我还是无法解决。

主要活动课

package com.deitel.realmrecyclerviewspinnerpriority;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
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.TextView;
import android.widget.Toast;

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

import java.util.ArrayList;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;
public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity";
    Realm realm;
    EditText text_name;
    EditText text_details;
    ImageButton btn_ascending,btn_descending;
    RecyclerView rv;
    Spinner spinner;
    TextView text_data;
    Button btn_save,btn_view;
    ArrayList<String> itemslist;
    AdapterClass addapter;
    @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);
        text_data=findViewById(R.id.text_data);
        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);
        btn_ascending=findViewById(R.id.btn_ascending);
        btn_descending=findViewById(R.id.btn_descneding);
        rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        final RelmHelper helper=new RelmHelper(realm);

        itemslist=helper.retrievedata();

        addapter = new AdapterClass(MainActivity.this,itemslist);
        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_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModelClass md=new ModelClass();
                md.setName(text_name.getText().toString());
                md.setDetails(text_details.getText().toString());

                /*save data*/
                RelmHelper helper=new RelmHelper(realm);
                helper.savedata(md);

            }
        });
    }
    }
```
MY REALM HELPER CLASS
```
    package com.deitel.realmrecyclerviewspinnerpriority.Database;

import android.view.Display;

import com.deitel.realmrecyclerviewspinnerpriority.Modelclass.ModelClass;

import java.util.ArrayList;

import io.realm.Realm;
import io.realm.RealmResults;

public class RelmHelper {
    Realm realm;

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

    public ArrayList<String> retrievedata(){
        ArrayList<String> modelclassdata=new ArrayList<>();
        RealmResults<ModelClass> modelClass=realm.where(ModelClass.class).findAll();
        for (ModelClass MD:modelClass){
            modelclassdata.add(MD.getName());
            modelclassdata.add(MD.getDetails());
//            modelclassdata.add(MD.getPriority());
        }
        return modelclassdata;
    }
}
```
Model Class
```
    package com.deitel.realmrecyclerviewspinnerpriority.Modelclass;

import io.realm.RealmObject;

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

    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;
//    }
}
```
Adapter Class
```[![enter image description here][1]][1]
    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.Modelclass.ModelClass;
import com.deitel.realmrecyclerviewspinnerpriority.R;

import java.util.ArrayList;
import java.util.List;

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.Viewholder> {
    ArrayList<String> modelClassList;
    Context context;
    public AdapterClass(@NonNull Context context,ArrayList<String> modelClassList){
        this.modelClassList=modelClassList;
        this.context=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));
        holder.textView_details.setText("Details : " + modelClassList.get(position));
//        holder.textView_priority.setText("Priority : " + modelClassList.get(position));
//        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                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);

        }
    }
}

enter image description here

0 个答案:

没有答案