2

I have an ActivityController that contains a bottom navigation and I want to create interface between RecyclerviewAdapter and Fragment that are inside the bottom navigation to send the adapter position.

I get this error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.my.money, PID: 10644
    java.lang.ClassCastException: com.my.money.Content.ControllerActivity cannot be cast to com.my.money.Content.ui.debt.AdapterInterface
        at com.my.money.Content.ui.debt.DebtFragment.onCreateView(DebtFragment.java:52)

DebtFragment :

public class DebtFragment extends Fragment implements View.OnClickListener, AdapterInterface {

    private FragmentDebtBinding binding;
    private ClassAdapterDebt adapter;
    private ArrayList<CustomListDebt> arrayList = new ArrayList<>();
    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentDebtBinding.inflate(inflater, container, false);
        View view = binding.getRoot();

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new ClassAdapterDebt(arrayList, getActivity(), (AdapterInterface) getActivity());
        binding.recyclerView.setAdapter(adapter);
        showAllData();

        return view;
    }

    ...

    @Override
    public void onClickItem(int position) {
        Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
    }

Interface

public interface AdapterInterface {
    void onClickItem(int position);
}

Adapter

private ArrayList<CustomListDebt> arrayList = new ArrayList<>();
private Context context;
private AdapterInterface adapterInterface;

// Constructor
public ClassAdapterDebt(ArrayList<CustomListDebt> arrayList, Context context, AdapterInterface anInterface) {
    this.arrayList = arrayList;
    this.context = context;
    this.adapterInterface = anInterface;
}

This in onBindViewHolder

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (adapterInterface != null) {
            adapterInterface.onClickItem(position);
        } else {
            adapterInterface.onClickItem(0);
        }
    }
});

My try

I try to change this

adapter = new ClassAdapterDebt(arrayList, getActivity(), (AdapterInterface) getActivity());

To this :

adapter = new ClassAdapterDebt(arrayList, getActivity(), (AdapterInterface) context);

Error cannot cast will disappear and replaced with error :

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.my.money, PID: 22316
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.my.money.Content.ui.debt.AdapterInterface.onClickItem(int)' on a null object reference
        at com.my.money.Content.ui.debt.ClassAdapterDebt$1.onClick(ClassAdapterDebt.java:68)

There are some notes during the build proccess, does this have any effect?

Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Users\asus\AndroidStudioProjects\PencatatKeuangan\app\src\main\java\com\my\money\Content\ui\Adapter\RecyclerHomeAdapter.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Any help will be very important

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Sasmita
  • 137
  • 1
  • 12

1 Answers1

0

I found a way. To make the interface between the adapter and the fragment in the navHost a little different

The logic I use

  1. First I create an InterfaceAdapter between the adapter and the activity_main.java used as the navHost controller.
  2. I sending data from activity_main.java to fragment using InterfaceActivity. The data comes from the InterfaceAdapter.

InterfaceAdapter

public interface AdapterInterface {
    void sendPosition(int position);
}

InterfaceActivity

public interface AdapterInterface {
    void sendPosition2(int position);
}

ClassAdapter

private ArrayList<CustomList> arrayList = new ArrayList<>();
private Context context;
private AdapterInterface adapterInterface;


// Edit the contructor
public ClassAdapter(Context context, ArrayList<CustomList> arrayList, AdapterInterface adapterInterface) {
    this.context = context;
    this.arrayList = arrayList;
    this.adapterInterface = adapterInterface;
}

// example send position when onClick, do this in onBindViewHolder
holder.itemView.setOnClickListener((view) -> {
    adapterInterface.sendPosition(position);
});

MainActivity

public class MainActivity extends AppCompatActivity implements AdapterInterface {

    private Bundle bundle;
    private InterfaceActivity interfaceActivity;

    // alt+enter and implement method

    @Override
    public void sendPosition(int position) {
        interfaceActivity.sendPosition2(position);
    }

    public void dataHome(InterfaceActivity interfaceActivity) {
        this.interfaceActivity = interfaceActivity;
    }

}

Fragment in navHost

Do this in onCreateView

arrayList1.addAll(arrayList);
binding.recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new ClassAdapter(getActivity(), arrayList1, (AdapterInterface) getActivity());
binding.recyclerview.setAdapter(adapter);

((MainActivity)getActivity()).dataHome(new InterfaceActivity() {
    @Override
    public void sendPosition(int position) {
        // do something
    }
});

Hope this will help someone

Sasmita
  • 137
  • 1
  • 12