Skip to content Skip to sidebar Skip to footer

Callback Method In List Adapter Doesn't Work

I used a callback methods I was guided to use in a previous question. It doesn't seem to work. The onClick() method is not called. callback methods seem to be a very broad concept.

Solution 1:

You don't need to pass view and position, just pass the object you want.

Change your interface parameter

publicinterfaceItemClickListener {
     voidonItemClick(Country country);
 }

and you can pass the object you need

publicvoidonClick(View view) {

     finalCountrycurrentCountry= countriesList.get(getAdapterPosition());

     mClickListener.onItemClick(currentCountry);
}

In your Activity, get your Country

@OverridepublicvoidonItemClick(Country country) {

      //  get country and do anything you want
 }

Hope this helps

Solution 2:

That's because you are missing @Override annotation inside your RowViewHolder's onClick method.

It should look like below.

classRowViewHolderextendsRecyclerView.ViewHolder implementsView.OnClickListener {

        TextView viewName;
        ImageView viewFlag;
        LinearLayout countryEntry;

        RowViewHolder(View view) {

            super(view);
            viewName = view.findViewById(R.id.name);
            viewFlag = view.findViewById(R.id.flag);
            countryEntry = view.findViewById(R.id.rowId);
            view.setOnClickListener(this);
        }

        @OverridepublicvoidonClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
            ListActivity.position = getAdapterPosition();
            finalCountrycurrentCountry= countriesList.get(getAdapterPosition());
            listActivity.setCurrentCountry(currentCountry);

        }
    }

Post a Comment for "Callback Method In List Adapter Doesn't Work"