-1

I am using 3 tabs and each of them have fragments. I need to bring this Activity to Fragment of tab. is this possible?

public class MActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);
    GridView gridView = (GridView) findViewById(R.id.grid_view);
    gridView.setAdapter(new ImageAdapter(this));
}

}

public class FragmentTab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.grid_layout, container, false);
    GridView gridView = (GridView) getActivity().findViewById(R.id.grid_view);


    return rootView;
}

Can someone help me edit the fragment class to bring the activity from Adapter. Thanks

Shamshid
  • 403
  • 3
  • 11

3 Answers3

1

got the right answer from here.

    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab_1,container,false);
    GridView gridView = (GridView) view.findViewById(R.id.grid_view);
    gridView.setAdapter(new ImageAdapter(view.getContext()));
    return view;
}
Community
  • 1
  • 1
Shamshid
  • 403
  • 3
  • 11
0

One not so clean but the simplest way is to do this:

 public class FragmentTab1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tab_1, container, false);
        activityAdapter = (MActivity)getActivity().getImageAdapter();
        return rootView;
    }

expose a adapter getter in your activity->

public class MActivity extends Activity {

    private ImageAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        GridView gridView = (GridView) findViewById(R.id.grid_view);
        this.adapter = new ImageAdapter(this);
        gridView.setAdapter(this.adapter);
    }

    public ImageAdapter getImageAdapter(){
        return this.adapter;
    }
}

The second method would be to create a constructor in your fragment, to take in the adapter object when the fragment is constructed. (This would be a much cleaner object)

shiladitya
  • 2,290
  • 1
  • 23
  • 36
  • I get errors at `activityAdapter = (MAcitivity)getActivity().getImageAdapter();` errors are at `acitivityAdapter` and `getImageAdapter` – Shamshid Sep 17 '15 at 18:16
  • try to force the brackets. it should work - ((MActivity)getActivity()).getImageAdapter() – shiladitya Sep 17 '15 at 18:21
  • still `activityAdapter` error is not fixed and now i get red underline from the open of 1st bracket to `getActivity())` as the error says `cannot cast 'android.support.v4.app.FragmentActivity' to com.example.app.MActivity` – Shamshid Sep 17 '15 at 18:25
  • okay.. then i would suggest you to follow the other answer.. using the constructor approach – shiladitya Sep 17 '15 at 18:29
0

If my understanding of your problem is correct, one way is to pass the activity/context object through constructors of your ViewPager Adapter and Fragments.

J28
  • 1,080
  • 3
  • 15
  • 25
  • Im looking for the corrected codes too for the fragment. – Shamshid Sep 17 '15 at 18:09
  • Hi.. what he is saying is that you just need to add a parameterized constructor. I don't think answers require explicit code if it is that obvious – shiladitya Sep 17 '15 at 18:11
  • 1
    Fragments need to have no parameter constructor through which they are recreated e.g. on screen rotation. Unless the fragment is retained, you'll lose everything that is not saved via `onSaveInstanceState` mechanism. – Eugen Pechanec Sep 17 '15 at 19:54