我正在尝试将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很新,我无法理解这个错误到底意味着什么,所以我甚至无法谷歌。
答案 0 :(得分:4)
IntelliJ提示说,构造函数的super
调用中的第一个参数应为Context
。
试试这个。
public SubjectsAdapter(MainActivity activity, OrderedRealmCollection<TimeStamp> data) {
super(activity, data, true);
this.activity = activity;
}