猿问

Android:RecyclerViewAdapter 中的 GetApplication

我想在 Recyclerview 的 onClickListener 方法中充气 Recyclerview。

为此,我知道我必须从相应的活动中获取上下文来设置 LinearLayoutManager。

问题: 由于我在 Fragment 中使用 Recyclerview 来尝试创建另一个 Recyclerview 我不知道如何获得正确的上下文。

我尝试了什么:

  • 在我的构造函数中使用 FragmentActivity mContext 稍后尝试通过“mContext.getApplicationContext()”获取上下文 (从另一篇文章中获取 FragmentActivity,事先使用 Context mContext)

代码中的问题: 我将尽可能好地分解我的代码:

  1. 从我尝试初始化 LinearLayoutManager 的第一个 Recyclerview 中找到代码

公共类 deckbuilder_RViewAdapter_Card 扩展 RecyclerView.Adapter {

public deckbuilder_RViewAdapter_Card(FragmentActivity mContext, List<Cards> mData) {

                this.mData = mData;

                this.mContext = mContext;

            }


 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( ... );


LinearLayoutManager layoutManagerCards = new LinearLayoutManager(mContext.getApplicationContext(), LinearLayoutManager.VERTICAL, false);

                rvList.setLayoutManager(layoutManagerCards);

                deckbuilder_RViewAdapter_List addCardAdapter = new deckbuilder_RViewAdapter_List(mContext, listCards);

                rvList.setAdapter(addCardAdapter);

}

        });



    }


public int getItemCount() {

        return mData.size();

    }


    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView ivCardImage;

        TextView tvCardName;


        public ViewHolder(@NonNull View introListView) {

            super(introListView);

            fab_deckbuilder_add = introListView.findViewById(R.id.fab_deckbuilder_add);

            fab_deckbuilder_remove = introListView.findViewById(R.id.fab_deckbuilder_remove);

            rvList = introListView.findViewById(R.id.rv_deckbuilder_list);


        }

    }


FFIVE
浏览 160回答 1
1回答

沧海一幻觉

我认为你只是rvList从错误的地方得到,它不应该在你的视图持有者中,它应该在你的片段中,这就是为什么当你点击它时它会抛出 NullPointerException 。首先,rvList = introListView.findViewById(R.id.rv_deckbuilder_list);从您的视图持有者中删除,因为它只会返回 null。接下来,更新您的deckbuilder_RViewAdapter_List适配器,使其支持数据更改:public deckbuilder_RViewAdapter_List (Context mContext, List<Decklist> mDecklist) {&nbsp; &nbsp; this.mDecklist = mDecklist;&nbsp; &nbsp; this.mContext = mContext;}public void setCards(List<Decklist> mDecklist) {&nbsp; &nbsp; this.mDecklist = mDecklist;&nbsp; &nbsp; 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) {&nbsp; &nbsp; this.mData = mData;&nbsp; &nbsp; this.mContext = mContext;&nbsp; &nbsp; this.mCardListAdapter = mCardListAdapter;}public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {&nbsp; &nbsp; fab_deckbuilder_add.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Decklist> listCards = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listCards.add(new Decklist( ... );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mCardListAdapter.setCards(listCards);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}最后,RecyclerView在片段中找到s,然后使用适当的适配器附加它们:@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {&nbsp; &nbsp; // ... after inflate view&nbsp; &nbsp; rvCards = view.findViewById(R.id.rv_deckbuilder_cards)&nbsp; &nbsp; rvList = view.findViewById(R.id.rv_deckbuilder_list)&nbsp; &nbsp; deckbuilder_RViewAdapter_List listAdapter = new deckbuilder_RViewAdapter_List(..)&nbsp; &nbsp; deckbuilder_RViewAdapter_Card cardAdapter = new deckbuilder_RViewAdapter_Card(.., listAdapter)&nbsp; &nbsp; rvCards.setAdapter(cardAdapter);&nbsp; &nbsp; rvList.setAdapter(listAdapter);&nbsp; &nbsp; // ...}
随时随地看视频慕课网APP

相关分类

Java
我要回答