user3227070 user3227070 - 4 months ago 16
Java Question

Interface adaptor to interface fragment android

Adaptor class that provides an interface:

public class Adaptor{

private ItemCLickCallback itemCLickCallback;

public interface ItemCLickCallback {
void onItemClick(int p);
void onSecItemClick(int p);
}
public void setItemCLickCallback(final ItemCLickCallback itemCLickCallback){
this.itemCLickCallback = itemCLickCallback;
}}


public class Overview extends Fragment implements Adaptor.ItemCLickCallback{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_overview, container, false);
Adaptor adaptor = new Adaptor(mijnwinkels, this.getActivity());
adaptor.setItemCLickCallback(this);
return view;
}

@Override
public void onItemClick(int p) {
}
@Override
public void onSecItemClick(int p) {
// On click: send data to database, remove data from database
// Needs methods that should be implemented in my activity, cant be linked to the activity
}}


So. The methods that are overwritten in my fragment need methods that should be located in my activity, those methods need the data from the fragment. Usually I would use an interface to do this, but these methods are already part of an interface from another class, so how can I put them in an interface so I can do everything from my activity? Thank you

Answer

Your question seems to mostly deal with removing duplicate code from the interface, so here's some suggestions.

You don't need to add a whole new Adaptor when you get your data in the onSuccess.

private ArrayList<Winkel> mijnwinkels;
private Adaptor mAdaptor;

onCreateView() { 
    //... Other code 
    mijnwinkels = new  ArrayList<Winkel>();
    // mRecyclerView =... 
    mAdaptor = new Adaptor(mijnwinkels, this.getActivity());
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    mRecyclerView.addItemDecoration(new VerticalSpace(30));
    mRecyclerView.setAdapter(adaptor);
    adaptor.setItemCLickCallback(this);
   // etc... 
} 

@Override
public void onSuccess(ArrayList<Winkel> winkels) {
    mijnwinkels.clear();
    mijnwinkels.addAll(winkels);
    mAdaptor.notifyDatasetChanged(); // something like this... Not sure how RecyclerView does it 
 }

If you make Winkel implements Parcelable and implement the necessary code for that, you remove some lines there.

@Override
public void onItemClick(int p) {
    Winkel winkel = (Winkel) mijnwinkels.get(p);

     Bundle detailsBundle = new Bundle();
    detailsBundle.putExtra(EXTRA_WINKEL,winkel);

    Details detail= new Details();
    detail.setArguments(detailsBundle);
    this.getFragmentManager().beginTransaction()
            .replace(R.id.mycontainer,detail,null)
            .addToBackStack(null)
            .commit();
  }

Though, really, all this can be written as its own method.

public static void showWinkel(Winkel winkel, FragmentManager fm) {
    Bundle detailsBundle = new Bundle();
    detailsBundle.putExtra(EXTRA_WINKEL,winkel);

    Details detail= new Details();
    detail.setArguments(detailsBundle);
    fm.beginTransaction()
        .replace(R.id.mycontainer,detail)
        .addToBackStack(null)
        .commit();
} 

And called from anywhere

@Override
public void onItemClick(int p) {
    showWinkel(mijnwinkels.get(p), this.getFragmentManager());
}