Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – FirebaseRecyclerAdapter:将Activity Context传递给ViewHolder大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
列表项中的每个操作都是从RecyclerView.ViewHolder处理的.实际上,我想将Activity上下文传递给FirebaseRecyclerAdapter中的ViewHolder,所以我尝试将Constructor添加到此ViewHolder,如下所示:

public class TodoViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.accbListItemHomE)
    AppCompatcheckBox mAppCompatcheckBox;
    @BindView(R.id.actvListItemHomE)
    AppCompatTextView mAppCompatTextView;
    @BindView(R.id.acibListItemHomE)
    AppCompatImageButton mAppCompatImageButton;
    private Context mContext;

    public TodoViewHolder(Context context,View itemView) {
        super(itemView);
        mContext = context;
        ButterKnife.bind(this,itemView);
    }

    public AppCompatcheckBox getmAppCompatcheckBox() {
        return mAppCompatcheckBox;
    }

    public AppCompatTextView getmAppCompatTextView() {
        return mAppCompatTextView;
    }

    public AppCompatImageButton getmAppCompatImageButton() {
        return mAppCompatImageButton;
    }

    @OncheckedChanged(R.id.accbListItemHomE)
    void oncheckBoxChange(CompoundButton compoundButton,Boolean checked) {
        TodoMasterModel todoMasterModel = (TodoMasterModel) compoundButton.getTag();
        todoMasterModel.getmTodoModel().setmIsCompleted(checked);
        ((HomeActivity) mContext).onDoneClick(todoMasterModel,AddEditDialogFragment.ACTION_Edit);
        Log.i("checkBox",todoMasterModel.toString());
    }

    @OnClick(R.id.actvListItemHomE)
    void ontextViewClick(View view) {
        TodoMasterModel todoMasterModel = (TodoMasterModel) view.getTag();
        ((HomeActivity) mContext).showAddEditDialog(todoMasterModel,AddEditDialogFragment.ACTION_Edit);
        Log.i("TextView",todoMasterModel.toString());
    }

    @OnClick(R.id.acibListItemHomE)
    void onImageButtonClick(View view) {
        TodoMasterModel todoMasterModel = (TodoMasterModel) view.getTag();
        FirebaseDatabase.geTinstance().getReference("todos").child(todoMasterModel.getmId()).SETVALue(todoMasterModel.getmTodoModel());
        Log.i("delete",todoMasterModel.toString());
    }
}

我为我的目的创建了我的类并修改了FirebaseRecyclerAdapter,如下所示:

public abstract class MyFirebaseAdapter<T,VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {

    private Context mContext;
    protected int mModelLayout;
    Class<T> mModelClass;
    Class<VH> mViewHolderClass;
    FirebaseArray mSnapshots;

    /**
     * @param modelClass      Firebase will marshall the data at a LOCATIOn into an instance of a class that you provide
     * @param modelLayout     This is the layout used to represent a single item in the list. you will be responsible for populaTing an
     *                        instance of the corresponding view with the data from an instance of modelClass.
     * @param viewHolderClass The class that hold references to all sub-views in an instance modelLayout.
     * @param ref             The Firebase LOCATIOn to watch for data changes. Can also be a slice of a LOCATIOn,using some
     *                        combination of <code>limit()</code>,<code>startAt()</code>,and <code>endAt()</code>
     */
    public MyFirebaseAdapter(Context context,Class<T> modelClass,@R_673_4407@lLayout,Class<VH> viewHolderClass,Query ref) {
        mContext = context;
        mModelClass = modelClass;
        mModelLayout = modelLayout;
        mViewHolderClass = viewHolderClass;
        mSnapshots = new FirebaseArray(ref);

        mSnapshots.setOnChangedListener(new FirebaseArray.onChangedListener() {
            @Override
            public void onChanged(EventType type,int index,int oldIndeX) {
                switch (typE) {
                    case Added:
                        notifyItemInserted(indeX);
                        break;
                    case Changed:
                        notifyItemChanged(indeX);
                        break;
                    case Removed:
                        notifyItemRemoved(indeX);
                        break;
                    case Moved:
                        notifyItemMoved(oldIndex,indeX);
                        break;
                    default:
                        throw new IllegalStateException("Incomplete CASE statement");
                }
            }
        });
    }

    /**
     * @param modelClass      Firebase will marshall the data at a LOCATIOn into an instance of a class that you provide
     * @param modelLayout     This is the layout used to represent a single item in the list. you will be responsible for populaTing an
     *                        instance of the corresponding view with the data from an instance of modelClass.
     * @param viewHolderClass The class that hold references to all sub-views in an instance modelLayout.
     * @param ref             The Firebase LOCATIOn to watch for data changes. Can also be a slice of a LOCATIOn,DatabaseReference ref) {
        this(context,modelClass,modelLayout,viewHolderClass,(Query) ref);
    }

    public void cleanup() {
        mSnapshots.cleanup();
    }

    @Override
    public int getItemCount() {
        return mSnapshots.getCount();
    }

    public T getItem(int position) {
        return parseSnapshot(mSnapshots.getItem(position));
    }

    /**
     * This method parses the DataSnapshot into the requested type. You can override it in subclasses
     * to do custom parsing.
     *
     * @param snapshot the DataSnapshot to extract the model from
     * @return the model extracted from the DataSnapshot
     */
    protected T parseSnapshot(DataSnapshot snapshot) {
        return snapshot.getValue(mModelClass);
    }

    public DatabaseReference getRef(int position) {
        return mSnapshots.getItem(position).getRef();
    }

    @Override
    public long getItemId(int position) {
        // https://stackoverflow.com/questions/5100071/whats-the-purpose-of-item-ids-in-android-listview-adapter
        return mSnapshots.getItem(position).getKey().hashCode();
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent,int viewTypE) {
        ViewGroup view = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(viewType,parent,falsE);
        try {
            Constructor<VH> constructor = mViewHolderClass.getConstructor(View.class);
            return constructor.newInstance(mContext,view);
        } catch (NoSuchMethodException E) {
            throw new RuntimeException(E);
        } catch (InvocationTargetException E) {
            throw new RuntimeException(E);
        } catch (InstantiationException E) {
            throw new RuntimeException(E);
        } catch (illegalaccessexception E) {
            throw new RuntimeException(E);
        }
    }

    @Override
    public void onBindViewHolder(VH viewHolder,int position) {
        T model = getItem(position);
        populateViewHolder(viewHolder,model,position);
    }

    @Override
    public int getItemViewType(int position) {
        return mModelLayout;
    }

    /**
     * Each time the data at the given Firebase LOCATIOn changes,this method will be called for each item that needs
     * to be displayed. The first two arguments correspond to the mLayout and mModelClass given to the constructor of
     * this class. The third argument is the item's position in the list.
     * <p>
     * Your implementation should populate the view using the data contained in the model.
     *
     * @param viewHolder The view to populate
     * @param model      The object containing the data used to populate the view
     * @param position   The position in the list of the view being populated
     */
    abstract protected void populateViewHolder(VH viewHolder,T model,int position);
}

由于这一行错误即将到来:

return constructor.newInstance(mContext,view);

但我得到NoSuchMethod异常.我知道我做错了.但我没有办法做到这一点.任何人都可以帮助我吗?

解决方法

您的ViewHolder子类需要一个只有View参数的构造函数.从 FirebaseUI documentation

public static class ChatHolder extends RecyclerView.ViewHolder {
    View mView;

    public ChatHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

原因是在this code,它只搜索单个签名.添加代码搜索ViewHolder(Context,View)构造函数可能是一个很好的补充.您可以为firebaseui github repo添加功能请求吗?

更新:feature request on Github为那些期待它的人.

本图文内容来源于网友网络收集整理提供,作为学习参使用,版权属于原作者。

大佬总结

以上是大佬教程为你收集整理的android – FirebaseRecyclerAdapter:将Activity Context传递给ViewHolder全部内容,希望文章能够帮你解决android – FirebaseRecyclerAdapter:将Activity Context传递给ViewHolder所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。