Fragment being used by two different activties

Asked

Viewed 47 times

2

I have a Ragment that is inflating a Recycler view, so far so good. The problem is that this Fragment needs to inflate into two different Rawer navigation, and when it comes to casting, I can only cast for one of them.

Is there any way that when it comes to casting, he can identify which class is calling Fragment, and cast that particular class?

Below is the code that is giving error.

comunicadosList = ((NvdResp) getActivity()).getSetComunicadosList(3);
    ComunicadosAdapter adapter = new ComunicadosAdapter(getActivity(), comunicadosList);
    recyclerView.setAdapter(adapter);

in addition to the Nvdresp class (which I cast to search my list for the items to be inflated) I also have the Nvdaluno class. I could create a generic class or something?

  • I think using this casting scheme for what you’re doing is not ideal. I think you should do it using interface. By the way, I also find it strange to use Fragments in a Recyclerview. Don’t know a custom view there?

  • Actually Fragment is inflating Recyclerview inside the Navigationdrawer. the problem is that my application works with two profiles student and responsible, and precisely this Fragment needs to be inflated in the two Navigationdrawer, only if I put in a Navigationdrawer (in the case of the Nvdresp example) the other Navigationdrawer (in the case of Nvdaluno) from the cast error, I was thinking of creating a new class and putting my List on it and casting for that class, then instantiating the class inside my Navigationdrawer.

  • I’ve never worked with Customview, I’ll give you a search, you know you can never have too much knowledge ; - )

1 answer

0


When a Fragment needs to access the Activity, he must force the Activity to implement an interface that defines these methods.

Thus the cast can be made for the interface and not for an implementation of a Activity in particular.

Start by declaring the interface:

public interface ComunicadosListProvider {
      public ComunicadosList getSetComunicadosList(int valor);

      //Nota: substitua ComunicadosList pelo tipo da sua lista
}  

and a field to store an object that implements it:

ComunicadosListProvider mComunicadosListProvider;

In the onAttach() of Fragment keep a reference to interface, to Activity has to implement this interface.

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {                           //cast feito para a interface
        mComunicadosListProvider = (ComunicadosListProvider) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
        + " deve implementar ComunicadosListProvider");
    }
}  

When you want to call the method use:

comunicadosList = mComunicadosListProvider.getSetComunicadosList(3);
ComunicadosAdapter adapter = new ComunicadosAdapter(getActivity(), comunicadosList);
recyclerView.setAdapter(adapter);
  • Thank you very much, I had no idea how to do that !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.