用于显示领域数据的通用适配器

时间:2016-12-20 21:21:44

标签: android realm

我正在尝试创建通用适配器以在RecyclerView上显示领域数据但我得到以下编译时错误

  

错误:(25,8)错误:SearchListAdapter不是抽象的,也不是   覆盖抽象方法   适配器

中的onBindViewHolder(SearchListAdapter.SearchListViewHolder,int)

我有两个类,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());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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,所以我只是使用它Realm提供的RealmResults的通用RecyclerView.Adapter:

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);
    }

编辑:要使视图持有者具有通用性,您需要为其添加一个通用参数。

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);
}