我正在制作一个提醒应用程序,我试图使用领域。我正在使用getInstance()。这是我得到的错误

时间:2018-01-20 14:04:04

标签: java android database realm

无法将io.realm.RealmConfiguration转换为android.content.Context。 这是我的活动类

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reminder);

    rvListings = (RecyclerView) findViewById(R.id.events);

    list = new ArrayList<ListingsModel>();

    // read the saved values in database


    Realm myRealm = Realm.getInstance(getBaseContext());
    results1 = myRealm.where(EventModelDB.class).findAll();

    for (int i = results1.size() - 1; i >= 0; i--) {
        EventModelDB c = results1.get(i);
        model = new ListingsModel();
        model.setEvent(c.getEvent());
        model.setTime(c.getTime());
        model.setDate(c.getDate());
        model.setTimestamp(c.getTimestamp());

        list.add(model);
    }


    adapter = new ListingsAdapter(list, getBaseContext());
    rvListings.setAdapter(adapter);
    mLayoutManager = new LinearLayoutManager(this);
    //   mLayoutManager.setReverseLayout(true);
    //  mLayoutManager.setStackFromEnd(true);
    rvListings.setLayoutManager(mLayoutManager);
}

这是我的ListingsAdapter。我在这里得到同样的错误。这是相关部分。我甚至尝试过使用这个

RealmConfiguration realmConfig1 = new 
RealmConfiguration.Builder(getContext()).name("1testSave1.realm").build();
Realm.deleteRealm(realmConfig1);
Realm myRealm = Realm.getInstance(realmConfig1);

    public class ListingsAdapter extends RecyclerView.Adapter<ListingsAdapter.Viewholder> {

    public ListingsAdapter(ArrayList<ListingsModel> data, Context context) {
    this.data = data;
    this.context = context;
}

    // long click an item to delete it from database
    holder.toplayout.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            // deleting the selected row from Realm database
            Realm realm = Realm.getInstance(context);
            RealmResults<EventModelDB> result = realm.where(EventModelDB.class)
                    .equalTo("timestamp", data.get(position).getTimestamp())
                    .findAll();

            if(result != null){
                if(result.size() > 0){
                    realm.beginTransaction();
                    result.remove(0);
                    realm.commitTransaction();

                }
            }

        }
    });


}

}

3 个答案:

答案 0 :(得分:1)

因此,假设您从build.gradle中删除了以下内容:

dependencies {
    // compile 'io.realm:realm-android:0.82.2`
}

并在项目级别构建gradle中将其替换为最新版本

classpath "io.realm:realm-gradle-plugin:4.3.3"

并应用插件

apply plugin: 'com.android.application'
// other plugins
apply plugin: 'realm-android'

现在我们可以谈谈这里有多少错误,所以首先我们应该添加

dependencies {
    implementation 'io.realm:android-adapters:2.1.1'
}

然后我们可以在Application类中做到

public class CustomApplication extends MultiDexApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this);
        Realm.setDefaultConfiguration(new RealmConfiguration.Builder()
                                              .name("1testSave1.realm")
                                              .build());
    }
}

然后在AndroidManifest.xml中

<application android:name=".CustomApplication"

然后

public class MyActivity extends AppCompatActivity {
    Realm realm;
    RealmResults<EventModelDB> results;
    RealmChangeListener<RealmResults<EventModelDB>> realmChangeListener = (results) -> {
        // actually, just keep this here
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder);

        rvListings = findViewById(R.id.events);

        realm = Realm.getDefaultInstance();
        results = realm.where(EventModelDB.class).findAllAsync();
        results.addChangeListener(realmChangeListener);
        adapter = new ListingsAdapter(realm, results);
        rvListings.setAdapter(adapter);
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvListings.setLayoutManager(mLayoutManager);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        results.removeAllChangeListeners();
        realm.close();
        results = null;
        realm = null;
    }
}

public class ListingsAdapter extends RealmRecyclerViewAdapter<EventModelDB, ListingsAdapter.Viewholder> {
    private final Realm realm;

    public ListingsAdapter(Realm realm, RealmResults<EventModelDB> results) {
        super(results, true);
        this.realm = realm;
    }

    @Override
    public void onBindViewHolder(Viewholder holder, int position) {
        EventModelDB event = getData().get(position);
        final long timestamp = event.getTimestamp();

        // long click an item to delete it from database
        holder.toplayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {        
                // deleting the selected row from Realm database
                realm.executeTransactionAsync((r) -> {
                    r.where(EventModelDB.class)
                        .equalTo("timestamp", timestamp)
                        .findAll()
                        .deleteAllFromRealm();
                });
            }
        });    
    }
}
砰的一声,修好了你的船

答案 1 :(得分:0)

setContentView(R.layout.activity_reminder);之后

添加:Realm.init(this);

答案 2 :(得分:0)

您正在将Android上下文传递给Realm

Realm myRealm = Realm.getInstance(getBaseContext());

这需要是一个RealmConfiguration。

RealmConfiguration myConfig = new RealmConfiguration();
// config myConfig
Realm myRealm = Realm.getInstance(myConfig);

有关详细信息,请参阅https://realm.io/docs/dotnet/latest/#realms