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,int modelLayout,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:java.lang.RuntimeException: java.lang.NoSuchMethodException:
[class androID.vIEw.VIEw]
at
com.letsnurture.androID.firebasedatabasedemo.adapter.MyFirebaseAdapter.onCreateVIEwHolder(MyFirebaseAdapter.java:120)
at
androID.support.v7.Widget.RecyclerVIEw$Adapter.createVIEwHolder(RecyclerVIEw.java:5779)
at
androID.support.v7.Widget.RecyclerVIEw$Recycler.getVIEwForposition(RecyclerVIEw.java:5003)
at
androID.support.v7.Widget.RecyclerVIEw$Recycler.getVIEwForposition(RecyclerVIEw.java:4913)
at
androID.support.v7.Widget.linearlayoutmanager$LayoutState.next(linearlayoutmanager.java:2029)
at
androID.support.v7.Widget.linearlayoutmanager.layoutChunk(linearlayoutmanager.java:1414)
at
androID.support.v7.Widget.linearlayoutmanager.fill(linearlayoutmanager.java:1377)
at
androID.support.v7.Widget.linearlayoutmanager.onLayoutChildren(linearlayoutmanager.java:578)
at
androID.support.v7.Widget.RecyclerVIEw.dispatchLayoutStep2(RecyclerVIEw.java:3260)
at
androID.support.v7.Widget.RecyclerVIEw.dispatchLayout(RecyclerVIEw.java:3069)
at
androID.support.v7.Widget.RecyclerVIEw.onLayout(RecyclerVIEw.java:3518)
at androID.vIEw.VIEw.layout(VIEw.java:16651)
at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:5440)
at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1743)
at
androID.Widget.linearLayout.layoutHorizontal(linearLayout.java:1732)
at androID.Widget.linearLayout.onLayout(linearLayout.java:1497)
at androID.vIEw.VIEw.layout(VIEw.java:16651)
at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:5440)
at
androID.support.design.Widget.headerScrollingVIEwBehavior.layoutChild(headerScrollingVIEwBehavior.java:120)
at
androID.support.design.Widget.VIEwOffsetBehavior.onLayoutChild(VIEwOffsetBehavior.java:42)
at
androID.support.design.Widget.AppbarLayout$ScrollingVIEwBehavior.onLayoutChild(AppbarLayout.java:1319)
at
androID.support.design.Widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:815)
at androID.vIEw.VIEw.layout(VIEw.java:16651)
at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:5440)
at
androID.support.v4.Widget.DrawerLayout.onLayout(DrawerLayout.java:1191)
at androID.vIEw.VIEw.layout(VIEw.java:16651)
at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:5440)
at androID.Widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at androID.Widget.FrameLayout.onLayout(FrameLayout.java:273)
at androID.vIEw.VIEw.layout(VIEw.java:16651)
at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:5440)
at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1743)
at androID.Widget.linearLayout.layoutVertical(linearLayout.java:1586)
at androID.Widget.linearLayout.onLayout(linearLayout.java:1495)
at androID.vIEw.VIEw.layout(VIEw.java:16651)
at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:5440)
at androID.Widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at androID.Widget.FrameLayout.onLayout(FrameLayout.java:273)
at androID.vIEw.VIEw.layout(VIEw.java:16651)
at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:5440)
at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1743)
at androID.Widget.linearLayout.layoutVertical(linearLayout.java:1586)
at androID.Widget.linearLayout.onLayout(linearLayout.java:1495)
at androID.vIEw.VIEw.layout(VIEw.java:16651)
at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:5440)
at androID.Widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at androID.Widget.FrameLayout.onLayout(FrameLayout.java:273)
at
com.androID.internal.policy.PhoneWindow$DecorVIEw.onLayout(PhoneWindow.java:2678)
at androID.vIEw.VIEw.layout(VIEw.java:16651)
at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:5440)
at androID.vIEw.VIEwRootImpl.performlayout(VIEwRootImpl.java:2183)
at androID.vIEw.VIEwRootImpl.performTraversals(VIEwRootImpl.java:1943)
at androID.vIEw.VIEwRootImpl.doTraversal(VIEwRootImpl.java:1119)
at
androID.vIEw.VIEwRootImpl$TraversalRunnable.run(VIEwRootImpl.java:6060)
at
androID.vIEw.Choreographer$CallbackRecord.run(Choreographer.java:858)
at androID.vi
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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)