我创建了两个RealmObjects:


  公共类HashtagObject扩展RealmObject
  
  公共类SearchtagObject扩展RealmObject


然后,我想对两个RealmObject使用一个适配器,我尝试使用通用添加RealmBaseAdapter,但这是不正确的:

public  class CustomListAdapter extends RealmBaseAdapter<T extends RealmObject> implements ListAdapter{

private final OrderedRealmCollection<T>  mItenName;
private Activity mContext;

public CustomListAdapter(Activity context, OrderedRealmCollection<T> realmResults){
    super(context, realmResults);
    this.mItenName=realmResults;
    this.mContext= context;
}}


如何将一个适配器与多个RealmObjects一起使用?

最佳答案

您的问题是您不应为此任务创建两个RealmObjects。您应该创建一个RealmObject。

public class HashtagObject extends RealmObject

public class SearchtagObject extends RealmObject


这些应该是

public class TagObject extends RealmObject {
    @Index
    private String type; //HASHTAG or SEARCHTAG




如果仍然需要分别显示它们,那么您将需要两个结果,在这种情况下,您肯定需要自定义适配器。

为此,我建议稍微镜像RealmRecyclerViewAdapter的作用。

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    RealmResults<HashtagObject> hashtags;
    RealmResults<SearchtagObject> searchtags;

    Realm realm;

    RealmChangeListener realmChangeListener = new RealmChangeListener() {
        @Override
        public void onChange(Object element) {
            notifyDataSetChanged();
        }
    }

    public SearchAdapter(Realm realm) {
        this.hashtags = realm.where(HashTagObject.class).findAllAsync();
        this.searchtags = realm.where(SearchtagObject.class).findAllAsync();

        this.hashtags.addChangeListener(realmChangeListener);
        this.searchtags.addChangeListener(realmChangeListener);
    }

    private <T extends RealmModel> int getResultSize(RealmResults<T> results) {
        return ((results == null) || (!results.isValid()) || (results.isValid() && !results.isLoaded()) ? 0 : results.size();
    }

    @Override
    public int getItemCount() {
        return 1 /* header */ + getResultSize(hashtags) + 1 /* header */ + getResultSize(searchtags);
    }

    static final int HEADER = 0;
    static final int HASH = 1;
    static final int SEARCH = 2;

    @Override
    public int getItemViewType(int position) {
        if(position == 0 || position == 1 /*header*/ + getResultSize(hashtags)) {
            return HEADER;
        }
        if(position < 1 /*header*/ + getResultSize(hashtags)) {
            return HASH;
        }
        return SEARCH;
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == HEADER) {
            return new HeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.search_header, parent, false));
        }
        if(viewType == HASH) {
            return new HashtagViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.search_hash, parent, false));
        }
        if(viewType == SEARCH) {
            return new SearchViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.search_search, parent, false));
        }
        throw new IllegalArgumentException("Invalid view type [" + viewType + "]");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder abstractHolder, int position) {
        if(abstractHolder instanceof HeaderViewHolder) {
            HeaderViewHolder holder = (HeaderViewHolder) abstractHolder;
            if(position == 0) {
                holder.setText(R.string.hash_header_text);
            } else {
                holder.setText(R.string.search_header_text);
            }
        } else if(abstractHolder instanceof HashtagViewHolder) {
            HashtagViewHolder holder = (HashtagViewHolder) abstractHolder;
            HashtagObject hashObject = hashtags.get(position - 1 /*header*/);
            holder.bind(hashObject);
        } else if(abstractHolder instanceof SearchViewHolder) {
            SearchViewHolder holder = (SearchViewHolder)abstractHolder;
            SearchtagObject searchObject = searchTags.get(
                 position - 1 /* header */
                          - 1 /* other header */
                          - getResultSize(hashtags) /* previous list */);
            holder.bind(searchObject);
        }
    }
}

09-28 13:13