Reputation: 70
Hi I am working with android. I had created viewpager app using fragments, Now I wanted to add constructor in my adpter class. I used get Activity() instead of context, but it shows an error. How can I do this???
Here is my code:
my 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;
}
}
Upvotes: 0
Views: 498
Reputation: 489
getActivity() is a function defined in Fragment class. You cannot use that inside your adapter.
So you need to give your fragmentActivity object as Context like
this.context= fragmentActivity;
Upvotes: 0
Reputation: 2246
use this.context = fragmentActivity;
instead of this.context = getActivity();
in adapter constructor
Upvotes: 1