-1

Problem in FragmentHome.this as in Activity it accepts ActivityName.this but in fragment it shows problem I have tried getContext and getActivity is shows error I wasn to pass value to recycle view

public class FragmentHome extends Fragment {
    private Fragment homefragment;

    public static final String BASE_URL = "http://10.0.2.2:3000";

    // product layout ....
    private RecyclerView recyclerView;

    //List<Product> productList = new ArrayList<>();

    public FragmentHome() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        //prouct view..code ...
        recyclerView = view.findViewById(R.id.showProducts);

        //making method for getting all products ...
        getAllProducts();

        return view;
    }

    private void getAllProducts(){
        Register register = ApiUrl.getInstance().create(Register.class);

        Call<List<Product>> productlist = register.getProducts(ApiUrl.session);

        productlist.enqueue(new Callback<List<Product>>() {
            @Override
            public void onResponse(Call<List<Product>> call, Response<List<Product>> response) {
                if (!response.isSuccessful()) {
                    Toast.makeText(getActivity(), "Code " + response.code(), Toast.LENGTH_SHORT).show();
                    return;
                }

                List<Product> productList = response.body();
                //Pass List to the Adapter class
                ProductAaptor productAaptor = new ProductAaptor(productList, FragmentHome.this);
                recyclerView.setAdapter(productAaptor);
                recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            }

            @Override
            public void onFailure(Call<List<Product>> call, Throwable t) {
                Toast.makeText(getContext(), "Error : " + t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 2
    Welcome to Stack Overflow. What is your actual problem? What is not working? What do you expect? – jbarat Jun 17 '19 at 16:18
  • 2
    So, whats the problem?? Code is not compiling? your app is crashing? you need to put more information – Shermano Jun 17 '19 at 16:19
  • 1
    What do you want to achieve by doing that? I hope doing `FragmentHome.this` should return the current `FragmentHome` object. is that what you want? – Gratien Asimbahwe Jun 17 '19 at 16:53

1 Answers1

0

[The base answer is taken from https://stackoverflow.com/a/52732620/9819031]
You can use onAttach() method to access the context for a Fragment

private Context mContext;

public FragmentHome() {
    // Required empty public constructor
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mContext=context;
}

Then you can use mContext parameter to access the context of the Fragment.

Gourav
  • 2,746
  • 5
  • 28
  • 45