0

this has became some thing complicated for me since im not so much familiar with fragments but it might also be simple for some of you guys, here i had this part of code referring to an activity, when i changed the activity to fragment it says can not cast from context to ListViewActivity, can you please help me solve this:

 @Override
public Filter getFilter() {
    return ((ListViewActivity)mContext).new ListFilter();
}

obviously mContext is a context reference.i understand that inside the fragment should get context with getActivity(), but from outside ?thanks a lot.

Dusan
  • 791
  • 5
  • 16
user3694470
  • 143
  • 1
  • 12

1 Answers1

1

I would construct a custom adapter similar like this:

public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;

public CustomBaseAdapter(Fragment fragment, List<RowItem> items) {
    this.context = fragment.getActivity();
    this.rowItems = items;
}
}

And in your fragment, call the adapter like this:

CustomBaseAdapter adapter = new CustomBaseAdapter(this, items);

Now you can cast the context in your adapter to ListViewActivity, assuming the fragment is part of ListViewActivity.

Hope this helps!

Robin
  • 709
  • 2
  • 8
  • 20
  • the filter i have put there, is for searching so i need to get context into the adapter – user3694470 Jun 14 '14 at 18:10
  • Yes, Context is just the Base Object. So, every Activity (as well as Application) derives from Context. For further information read this: http://stackoverflow.com/questions/6518206/what-is-the-difference-between-activity-and-context – Robin Jun 14 '14 at 21:04