如何在RecyclerView中使用Realm Android Adapter
作者:互联网
我正在尝试学习将RecyclerView与Realm结合使用.我正在从here开始关注官方教程.
但是当我自己编写程序时,我在复制示例本身时遇到错误.
MyRecyclerViewAdapter官方示例代码发布在Realm的Github上here.
我的SubjectsAdapter代码是-
package in.medicalguru.adapters;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import in.medicalguru.MainActivity;
import in.medicalguru.R;
import in.medicalguru.models.TimeStamp;
import io.realm.OrderedRealmCollection;
import io.realm.RealmRecyclerViewAdapter;
public class SubjectsAdapter extends RealmRecyclerViewAdapter<TimeStamp, SubjectsAdapter.MyViewHolder> {
private final MainActivity activity;
public SubjectsAdapter(MainActivity activity, OrderedRealmCollection<TimeStamp> data) {
super(data, true);
this.activity = activity;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TimeStamp obj = getData().get(position);
holder.data = obj;
//holder.title.setText(obj.getSubjectName());
holder.title.setText(obj.getTimeStamp());
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
public TextView title;
public TimeStamp data;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.textview);
view.setOnLongClickListener(this);
}
@Override
public boolean onLongClick(View v) {
//activity.deleteItem(data);
return true;
}
}
}
项目的build.grade的相关部分:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath "io.realm:realm-gradle-plugin:2.2.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
模块的build.gradle的相关部分:
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.google.android.gms:play-services-ads:10.0.1'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.4'
compile 'org.glassfish:javax.annotation:10.0-b28'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'io.realm:android-adapters:1.4.0'
}
不幸的是,我对Realm并不陌生,我无法理解该错误的确切含义,所以我什至无法在Google上找到它.
解决方法:
IntelliJ提示说,构造函数的超级调用中的第一个参数应为Context.
尝试这个.
public SubjectsAdapter(MainActivity activity, OrderedRealmCollection<TimeStamp> data) {
super(activity, data, true);
this.activity = activity;
}
标签:recycler-adapter,android-recyclerview,realm,adapter,android 来源: https://codeday.me/bug/20191026/1936710.html