RealmViewHolder没有从Abstract RealmAdapter获取RealmResults

时间:2016-06-10 20:08:39

标签: android-recyclerview realm

我一直在尝试将PirateApp的解决方案应用于:Best practices to use realm with a recycler view?

这是我的AbstractMutable:

public abstract class SessionAbstractMutableRealmAdapter<Session extends RealmObject,
    SessionViewHolder extends RecyclerView.ViewHolder>
    extends AbstractRealmAdapter<Session, SessionViewHolder> implements OnSwipeListener {

public Realm realm;

public SessionAbstractMutableRealmAdapter(Realm realm) {
    //call the superclass constructor to load data from subclasses into realmresults
    super(realm);
    this.realm = realm;
}

@Override
public final void onSwipe(int position) {
    if (!isHeader(position) && !isFooter(position) && !mResults.isEmpty()) {
        int itemPosition = position - getHeaderCount();
        realm.beginTransaction();
        Session item = mResults.get(itemPosition);
        item.deleteFromRealm();
        realm.commitTransaction();
        notifyItemRemoved(position);
    }
}

和AbstractRealmAdapter

public abstract class SessionAbstractRealmAdapter<Session extends RealmObject,
    SessionViewHolder extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<SessionViewHolder> {

    public static final int HEADER_COUNT = 1;
    public static final int FOOTER_COUNT = 1;

    //Our data source
    public RealmResults<Session> mResults;

    public SessionAbstractRealmAdapter(Realm realm) {
        //load data from subclasses
        mResults = loadData(realm);
        notifyDataSetChanged();
    }


public int getHeaderCount() {
    return hasHeader() ? HEADER_COUNT : 0;
}

public int getFooterCount() {
    return hasFooter() ? FOOTER_COUNT : 0;
}

public boolean isHeader(int position) {
    if (hasHeader()) {
        return position < HEADER_COUNT;
    } else {
        return false;
    }
}

public boolean isFooter(int position) {
    if (hasFooter()) {
        return position >= getCount() + getHeaderCount();
    } else {
        return false;
    }
}

@Override
public long getItemId(int i) {
    return i;
}


@Override
public final int getItemViewType(int position) {
    if (isHeader(position)) {
        return ItemType.HEADER.ordinal();
    } else if (isFooter(position)) {
        return ItemType.FOOTER.ordinal();
    } else {
        return ItemType.ITEM.ordinal();
    }
}

/**
 * @param position the position within our adapter inclusive of headers,items and footers
 * @return an item only if it is not a header or a footer, otherwise returns null
 */
public Session getItem(int position) {
    if (!isHeader(position) && !isFooter(position) && !mResults.isEmpty()) {
        return mResults.get(position - getHeaderCount());
    }
    return null;
}


@Override
public final int getItemCount() {
    return getHeaderCount() + getCount() + getFooterCount();
}

public final int getCount() {
    return mResults.size();
}

public abstract boolean hasHeader();

public abstract boolean hasFooter();


public void setData(RealmResults<Session> results) {
    mResults = results;
    notifyDataSetChanged();
}

protected abstract RealmResults<Session> loadData(Realm realm);

public enum ItemType {
    HEADER, ITEM, FOOTER;
}

}

然后我的ViewHolder以:

开头
public class SessionViewHolder extends RecyclerView.ViewHolder{

TextView sessionNumberTextView;
TextView passFailTextView;

直到我找到onBindViewHolder:

 public void onBindViewHolder(SessionViewHolder holder, int position) {

    Session session1 =  mResults.get(position)

    holder.sessionNumberTextView.setText(String.valueOf(session1.getSessionId()));

这里mResults是红色的,我认为,在视图持有者中无法访问mResults。任何关于我的错误的想法都将不胜感激。

另一方面,我认为SessionAbstractRealmAdapter是抽象的,无法实现,这看起来很明显,但不幸的是我不知道如何解决它。我有点新,在这方面学习。

   sAdapter = new SessionAbstractRealmAdapter(realmResults);
    recyclerView.setAdapter(sAdapter);

非常感谢任何帮助。我已经为我的领域编写了代码,但是当事情崩溃时我想建立一个刷卡删除功能。

0 个答案:

没有答案