gpt4 book ai didi

android - 显示 Realm 数据的通用适配器

转载 作者:行者123 更新时间:2023-11-30 00:49:46 25 4
gpt4 key购买 nike

我正在尝试创建通用适配器以在 RecyclerView 上显示 Realm 数据,但出现以下编译时错误

Error:(25, 8) error: SearchListAdapter is not abstract and does not override abstract method onBindViewHolder(SearchListAdapter.SearchListViewHolder,int) in Adapter

我有两个类,即 ObservationInfoCallReasonInfo 派生自 RealmObject

访问适配器的用法如下

访问观察信息

RealmResults<ObservationInfo> observationInfos = realm.where(ObservationInfo.class).equalTo("productCategory",lookupValue).findAll();
adapter = new SearchListAdapter(observationInfos,lookupType);
rvSearchItemListView.setAdapter(adapter);

访问 CallReason 信息

RealmResults<CallReasonInfo> callReasonInfos = realm.where(CallReasonInfo.class).equalTo("callStatusName",lookupValue).findAll();
adapter = new SearchListAdapter(callReasonInfos,lookupType);
rvSearchItemListView.setAdapter(adapter);

这是我的代码

public class SearchListAdapter <E extends RealmResults>  extends RecyclerView.Adapter<SearchListAdapter.SearchListViewHolder> {


private E lookupInfo;
private String lookupType;
public SearchListAdapter(E resultInfo, String lookupType) {
this.lookupType = lookupType;
this.lookupInfo = resultInfo;
}

@Override
public SearchListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.seach_list_item, parent, false);

SearchListAdapter.SearchListViewHolder viewHolder = new SearchListAdapter.SearchListViewHolder(v);

return viewHolder;

}

@Override
public void onBindViewHolder(SearchListViewHolder holder, int position) {
holder.bind(lookupInfo.get(position));
}

@Override
public int getItemCount() {
return lookupInfo==null ? 0 : lookupInfo.size();
}

public class SearchListViewHolder extends RecyclerView.ViewHolder {

private TextView tvItemName;

public SearchListViewHolder(View itemView) {
super(itemView);

tvItemName = (TextView) itemView.findViewById(R.id.tvItemName);


}

public void bind(RealmModel item){



tvItemName.setText(((ObservationInfo)item).getObservationName());
}
}
}

最佳答案

public class SearchListViewHolder extends RecyclerView.ViewHolder {

这应该是一个静态内部类

public static class SearchListViewHolder extends RecyclerView.ViewHolder {

此外,我认为这是错误的:

public class SearchListAdapter <E extends RealmResults>  extends RecyclerView.Adapter<SearchListAdapter.SearchListViewHolder> {    
private E lookupInfo;

应该是

public class SearchListAdapter<E extends RealmModel>  extends RecyclerView.Adapter<SearchListAdapter.SearchListViewHolder> {    
private RealmResults<E> lookupInfo;

但我对此有点怀疑,因为它不处理 Realm 中的更改,它不添加也不删除 RealmChangeListener,所以我宁愿只使用通用的 RecyclerView。 Realm 提供的 RealmResults 适配器:

https://github.com/realm/realm-android-adapters

添加为

repositories {
jcenter()
}

dependencies {
compile 'io.realm:android-adapters:1.4.0'
}

使用

public class SearchListAdapter<E extends RealmModel>  extends RealmRecyclerViewAdapter<E, SearchListAdapter.SearchListViewHolder> {
public SearchListAdapter(Context context, RealmResults<E> results) {
super(context, results, true);
}

编辑:要使 View 持有者通用,您需要向其添加一个通用参数。

public class SearchListAdapter<E extends RealmModel>  extends RealmRecyclerViewAdapter<E, SearchListViewHolder<E>> {
// ...

}

public abstract class SearchListViewHolder<E extends RealmModel> extends RecyclerView.ViewHolder {
public SearchListViewHolder(View view) {
super(view);
}

public abstract void bind(E e);
}

关于android - 显示 Realm 数据的通用适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41251453/

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