1

trying to start fragment from adapter class using this code

public void startFragment(Context context, int position) {
    Activity activity = (Activity) context;
    android.app.FragmentManager manager = activity.getFragmentManager();
    android.app.FragmentTransaction fragmentTransaction = manager.beginTransaction();
    DepartmentDetails departmentDetails = new DepartmentDetails();

    Bundle bundle = new Bundle();
    bundle.putString("id", departmentList.get(position).id);
    bundle.putSerializable("details", departmentList);
    departmentDetails.setArguments(bundle);

    fragmentTransaction.add(R.id.container, departmentDetails);
    fragmentTransaction.commit();
}

but it underlines red on fragmentTransaction.add(R.id.container, departmentDetails);

and while executing it shows

Error:(90, 28) error: no suitable method found for add(int,DepartmentDetails)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment)

here is fragment i am using..

public class DepartmentDetails extends Fragment implements CallBacks{

    ImageView ivProfile;
    TextView tvName, tvBasicInfo, tvDescription;
    String id, dean;
    Departments details;
    ArrayList<String> faculty, programs;

    RequestParams params;
    MyHttp myHttp;
    String url;

    public DepartmentDetails() {
        // Required empty public constructor
    }
Asad
  • 1,241
  • 3
  • 19
  • 32

3 Answers3

1

You can open fragment from adapter class using below code;

  Fragment fragment = new MyFragment();
  FragmentManager fm = context.getSupportFragmentManager();
  FragmentTransaction ft = fm.beginTransaction();
  ft.replace(R.id.content_frame, fragment);
  ft.commit();
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
1

Add in your DepartmentDetails fragment class

import android.support.v4.app.Fragment ;

instead of

  import  android.app.Fragment
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

Please try with following code.

  Fragment fragment = new MyFragment();
  FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
  FragmentTransaction ft = fm.beginTransaction();
  ft.replace(R.id.content_frame, fragment);
  ft.commit();
Sunil P
  • 3,698
  • 3
  • 13
  • 20
  • may be now if `getSupportFragmentManager` is not coming make it as `context.getSUpportFragmentManager` – Sunil P Aug 29 '17 at 09:20