1 回答
TA贡献1824条经验 获得超5个赞
我认为你只是rvList从错误的地方得到,它不应该在你的视图持有者中,它应该在你的片段中,这就是为什么当你点击它时它会抛出 NullPointerException 。
首先,rvList = introListView.findViewById(R.id.rv_deckbuilder_list);从您的视图持有者中删除,因为它只会返回 null。
接下来,更新您的deckbuilder_RViewAdapter_List适配器,使其支持数据更改:
public deckbuilder_RViewAdapter_List (Context mContext, List<Decklist> mDecklist) {
this.mDecklist = mDecklist;
this.mContext = mContext;
}
public void setCards(List<Decklist> mDecklist) {
this.mDecklist = mDecklist;
notifyDataSetChanged();
}
并更新您的deckbuilder_RViewAdapter_Card适配器,以便它可以参考deckbuilder_RViewAdapter_List:
private deckbuilder_RViewAdapter_List mCardListAdapter;
public deckbuilder_RViewAdapter_Card(FragmentActivity mContext, List<Cards> mData, deckbuilder_RViewAdapter_List mCardListAdapter) {
this.mData = mData;
this.mContext = mContext;
this.mCardListAdapter = mCardListAdapter;
}
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
fab_deckbuilder_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Decklist> listCards = new ArrayList<>();
listCards.add(new Decklist( ... );
mCardListAdapter.setCards(listCards);
}
});
}
最后,RecyclerView在片段中找到s,然后使用适当的适配器附加它们:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// ... after inflate view
rvCards = view.findViewById(R.id.rv_deckbuilder_cards)
rvList = view.findViewById(R.id.rv_deckbuilder_list)
deckbuilder_RViewAdapter_List listAdapter = new deckbuilder_RViewAdapter_List(..)
deckbuilder_RViewAdapter_Card cardAdapter = new deckbuilder_RViewAdapter_Card(.., listAdapter)
rvCards.setAdapter(cardAdapter);
rvList.setAdapter(listAdapter);
// ...
}
添加回答
举报