gpt4 book ai didi

android - 如何将 Realm Android Adapter 与 RecyclerView 一起使用

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:11 26 4
gpt4 key购买 nike

我正在尝试学习将 RecyclerViewRealm 结合使用。我正在关注 here 的官方教程.

但是当我自己编写程序时,我在复制示例本身时遇到错误。

Realm RecyclerView Adapter Error

官方MyRecyclerViewAdapter示例代码发布here在 Realm 的 Github 上。

我的 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 的新手,我无法理解这个错误的确切含义,所以我什至无法用谷歌搜索它。

最佳答案

IntelliJ 提示说构造函数的 super 调用中的第一个参数应该是 Context

试试这个。

public SubjectsAdapter(MainActivity activity, OrderedRealmCollection<TimeStamp> data) {
super(activity, data, true);
this.activity = activity;
}

关于android - 如何将 Realm Android Adapter 与 RecyclerView 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40961163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com