在Realm更改后,MVP方式实现了RealmRecyclerViewAdapter无法更新

时间:2017-05-22 19:53:24

标签: java android android-recyclerview realm mvp

RealmRecyclerViewAdapter(https://github.com/realm/realm-android-adapters)出了问题。 为了使事情变得更复杂,我实现了RecycleViewAdapter的MVP方法,它为各个项目创建了演示者。 MvpRealmRecyclerAdapter看起来像这样:

notify_send = request.user.profile.get_absolute_url()
notify.send(notify_send, recipient=pending_like, verb='Sent you a friend request' )

AttachmentAdapter的实现如下所示:

class Profile(models.Model):
    user = models.OneToOneField(User)
    location = models.CharField(max_length=120, choices=LOCATIONS,null=True, blank=True)
    picture = models.ImageField(upload_to=upload_location, null=True, blank=True)


    def __unicode__(self): 
        return self.user.username


    def get_absolute_url(self):
        url = reverse("profile", kwargs={"username": self.user.username})
        return url

    def like_link(self):
        url = reverse("like_user", kwargs={"id": self.user.id})
        return url`

当用户按下“添加附件”项(大+号)时,我从我的适配器调用此方法:

    public abstract class MvpRealmRecyclerAdapter<M extends RealmObject, P extends ModelPresenter, VH extends MvpViewHolder<P>> extends RealmRecyclerViewAdapter<M, VH> {
        protected final Map<Object, P> presenters;

        public MvpRealmRecyclerAdapter(){
            super(new RealmList<M>(), true);
            presenters = new HashMap<>();
        }

        public MvpRealmRecyclerAdapter(@Nullable OrderedRealmCollection<M> data, boolean autoUpdate) {
            super(data, autoUpdate);
            presenters = new HashMap<>();
        }

        public MvpRealmRecyclerAdapter(@Nullable OrderedRealmCollection<M> data) {
            super(data, true);
            presenters = new HashMap<>();
        }

        @NonNull
        protected P getPresenter(@NonNull M model) {
            System.err.println("Getting presenter for item " + getModelId(model));
            return presenters.get(getModelId(model));
        }

        @NonNull
        protected abstract P createPresenter(@NonNull M model);

        @NonNull
        protected abstract long getModelId(@NonNull M model);

        @Override
        public void onViewRecycled(VH holder) {
            super.onViewRecycled(holder);

            holder.unbindPresenter();
        }

        @Override
        public boolean onFailedToRecycleView(VH holder) {
            // Sometimes, if animations are running on the itemView's children, the RecyclerView won't
            // be able to recycle the view. We should still unbind the presenter.
            holder.unbindPresenter();

            return super.onFailedToRecycleView(holder);
        }

        @Override
        public void onBindViewHolder(VH holder, int position) {
            holder.bindPresenter(getPresenter(getItem(position)));
        }
    }

此方法启动CreationActivity,当用户选择要用作附件的文件时,将调用此方法:

    public class AttachmentAdapter extends MvpRealmRecyclerAdapter<Attachment, AttachmentPresenter, AttachmentHolderView> {
        private Context mContext;
        public boolean mReadOnly;
        private int limit;

        public void setLimit(int limit) {
            this.limit = limit;
        }

        public AttachmentAdapter(Context context, @NonNull OrderedRealmCollection<Attachment> attachments) {
            super(attachments, true);
            initialize(context, false);
        }

        public AttachmentAdapter(Context context, @NonNull OrderedRealmCollection<Attachment> attachments, boolean readonly) {
            super(attachments, true);
            initialize(context, readonly);
        }

        private void initialize(Context context, boolean readonly){
            setHasStableIds(true);
            mContext = context;
            mReadOnly = readonly;
        }

        @Override
        public AttachmentHolderView onCreateViewHolder(ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);

            // Inflate the custom layout
            View contactView = inflater.inflate(R.layout.item_attachment, parent, false);

            // Return a new holder instance
            return new AttachmentHolderView(contactView);
        }

        @Override
        public void onBindViewHolder(AttachmentHolderView holder, int position) {
            // Binding viewholder
        }

        @NonNull
        @Override
        protected AttachmentPresenter createPresenter(@NonNull Attachment model) {
            AttachmentPresenter presenter = new AttachmentPresenter();
            presenter.setModel(model);
            return presenter;
        }

        @NonNull
        @Override
        protected long getModelId(@NonNull Attachment model) {
            return model.getId();
        }

        // Some more code
    }

此活动有自己的演示者添加附件,演示者添加附件方法如下:

    private void getAttachmentImageFile(){
    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.MEDIA_CONTENT_CONTROL) == PackageManager.PERMISSION_GRANTED) {

        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        intent.setType("image/*");
        intent.putExtra("return-data", true);
        ((AppCompatActivity)mContext).startActivityForResult(intent, CreationActivity.REQUEST_IMAGE_SELECT);
        return;
    }

    ActivityCompat.requestPermissions(((AppCompatActivity)mContext),
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            OpportunityCreationActivity.REQUEST_PERMISSION_IMAGE
    );

}

此方法执行将附件添加到域的事务。领域回收视图监听领域更改并应通知新更改,但我得到此异常:

@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
    if (resultCode != RESULT_OK) {
        return;
    }

    switch (requestCode) {
        case RESULT_FILE_SELECTED:
            presenter.addAttachment(data.getData());
            break;
        default:
            break;
    }
}

据我所知,问题可能在于线程,这个领域是从不同的线程改变而不是回收视图正在监听,但可能我错了。我知道这一切看起来都很神秘而复杂,但情况就是这样。

0 个答案:

没有答案