我想将我的数据存储在android studio的“领域数据库”中,但不起作用?

时间:2019-12-24 07:03:42

标签: realm

希望您一切都好。 我想将我的数据存储在领域中,但无法正常工作,当我单击添加添加Toast的保存按钮时显示错误。没有发现错误但数据没有保存,请帮助我。我想将我的数据存储在领域中,但它不起作用,当我单击添加toast的保存按钮时,它向我显示错误。找不到错误,但数据没有保存,请帮帮我。

我的MainActicty类

    package com.deitel.realmrecyclerviewspinnerpriority;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.deitel.realmrecyclerviewspinnerpriority.Modelclass.ModelClass;

import io.realm.Realm;

public class MainActivity extends AppCompatActivity {

    private static final String TAG="MainActivity";
    Realm realm;
    EditText text_name;
    EditText text_details;
    Button btn_save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        realm=Realm.getDefaultInstance();
        Realm.init(getApplicationContext());
        text_name = findViewById(R.id.text_name);
        text_details = findViewById(R.id.text_details);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                savedata(text_name.getText().toString(),text_details.getText().toString());
            }
        });
    }
    private void savedata(final String name, final String datails){
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgrealm) {
//                Number maxid=bgrealm.where(ModelClass.class).max("id");
//                int newkey=(maxid==null) ? 1 : maxid.intValue()+1;
                ModelClass user = bgrealm.createObject(ModelClass.class);
                user.setName(name);
                user.setDetails(datails);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Toast.makeText(MainActivity.this, "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 configuration=new RealmConfiguration.Builder().name("RealmData.realm").build();
        Realm.setDefaultConfiguration(configuration);
    }
}

模型类。

    package com.deitel.realmrecyclerviewspinnerpriority.Modelclass;

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

public class ModelClass extends RealmObject {
    @PrimaryKey
    private String Name;
    private String Details;
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getDetails() {
        return Details;
    }

    public void setDetails(String details) {
        Details = details;
    }
}



1 个答案:

答案 0 :(得分:0)

我认为您没有设置主键,

尝试以下方法

private void savedata(final String name, final String datails){
        try(Realm mRealm = Realm.getDefaultInstance()){

            ModelClass user = new ModelClass();
            int newkey=(maxid==null) ? 1 : maxid.intValue()+1;
            user.setId(newkey);
            user.setName(name);
            user.setDetails(datails);

            mRealm.executeTransaction(rlm-> {
                rlm.insertOrUpdate(user);
            });
        }

    }

insertOrUpdate如果PK不存在则插入新记录,否则进行更新。