本文介绍了带有多个RealmObjects的RealmBaseAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个RealmObjects:

lockquote
public class HashtagObject extends RealmObject

公共类SearchtagObject扩展RealmObject


然后,我想为两个RealmObjects使用一个适配器,我尝试使用泛型添加RealmBaseAdapter,但它不正确:

  public class CustomListAdapter extends RealmBaseAdapter< T extends RealmObject>实现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


$ b $ p
$ b $ p $ public class TagObject扩展RealmObject {
@Index
私有字符串类型; // HASHTAG或SEARCHTAG






如果您仍然需要然后你需要两个结果,在这种情况下,你肯定会需要你的自定义适配器。



为此,我建议稍微镜像一下 RealmRecyclerViewAdapter

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

领域;

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


$ b $公共SearchAdapter(领域){
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));
}
抛出新的IllegalArgumentException(无效的视图类型[+ viewType +]);

$ b @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);
}
}
}


I created two RealmObjects:

Then I want to use one adapter for two RealmObjects, I try to add RealmBaseAdapter with generic but it's not correct:

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

How do I use one adapter with multiple RealmObjects?

解决方案

Your problem is that you shouldn't create two RealmObjects for this task. You should create one RealmObject.

public class HashtagObject extends RealmObject

public class SearchtagObject extends RealmObject

These should be

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


If you still need to show them separately, then you'll need two results, in which case you'll definitely need your custom adapter.

For this, I'd recommend somewhat mirroring what RealmRecyclerViewAdapter does.

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

这篇关于带有多个RealmObjects的RealmBaseAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 03:07