[英]I cannot use my arraylist in class fragment
我嘗試在片段中使用 arraylist 我的 arraylist 在片段 class 中不起作用。 在我在主要活動中使用此代碼之前請幫我解決這個問題,然后我添加菜單片段我將此代碼移動到此片段
然后我的arraylist得到錯誤
此錯誤“'android.widget.RelativeLayout'中的'RelativeLayout(anroid.content.Context)'不能應用於'(com.cupaxxhd.mysurah.HomeFragment)'”
'com.cupaxxhd.mysurah.Myadapter' 中的這個“'MyAdapter(android.content.Context, java.util.Arraylist)' 不能應用於'(com.cupaxxhd.mysurah.HomeFragment, Z93F725A07423FE1C889F448B33Dutil)'2
public class HomeFragment extends Fragment {
RecyclerView mRecyclerView;
MyAdapter myAdapter;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
mRecyclerView = v.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new RelativeLayout(this));
myAdapter = new MyAdapter(this, getMyList());
mRecyclerView.setAdapter(myAdapter);
return v;
}
private ArrayList<Model> getMyList(){
ArrayList<Model> models = new ArrayList<>();
Model m = new Model();
m.setTitle("Al-Lahab");
m.setDescription("Surah Al-Lahab adalah surat ke-111 dalam Al-Qur'an.");
m.setDetails("تَبَّتْ يَدَا أَبِي لَهَبٍ وَتَبّ\n"
);
m.setImg(R.drawable.al_lahab);
models.add(m);
return models;
}
您需要傳遞 Context 而不是片段實例,並使用 LayoutManager 而不是 RelativeLayout。 像這樣:
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
new MyAdapter(getActivity(), getMyList());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.