您好我正在使用android。我使用片段创建了viewpager app,现在我想在adpter类中添加构造函数。我使用了get Activity()而不是context,但它显示了一个错误。我怎么能这样做?
这是我的代码:
我的adapter.java
public Swipe_adapter(FragmentActivity fragmentActivity, String[] appname,
String[] desc, int[] logo, int[] button, int[] rating) {
this.context = getActivity();
this.appname = appname;
this.desc = desc;
this.logo = logo;
this.button = button;
this.rating=rating;
}
My Fragment class
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_featured, container, false);
appname = new String[] { "app1", "app2", "app3", "app4", "app5", "app6", "app7", "app8", "app9", "app10" };
desc = new String[] { "ssddds", "sdsad", "sdsadsadsda",
"Ijhgkhj", "ghghjghj", "ghjghjgh", "gjghjgh", "gjhghjgj",
"ghjghjg64", "Juiououo" };
logo = new int[] { R.drawable.app1, R.drawable.app2,
R.drawable.app3, R.drawable.app4,
R.drawable.app5, R.drawable.app6, R.drawable.app8,
R.drawable.app1, R.drawable.app2, R.drawable.app3 };
button = new int[] { R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download, R.drawable.download };
rating = new int[] { R.drawable.star, R.drawable.star,
R.drawable.star, R.drawable.star,
R.drawable.star, R.drawable.star, R.drawable.star,
R.drawable.star, R.drawable.star, R.drawable.star };
ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.view_pager);
adapter = new Swipe_adapter(getActivity(), appname,desc,logo,button,rating);
viewPager.setAdapter(adapter);
return rootView;
}
}
答案 0 :(得分:1)
在适配器构造函数
中使用this.context = fragmentActivity;
而不是this.context = getActivity();
答案 1 :(得分:0)
getActivity()是Fragment类中定义的函数。你不能在你的适配器中使用它。
所以你需要将fragmentActivity对象作为上下文提供
this.context= fragmentActivity;