Callback method in list Adapter doesn't work

回眸只為那壹抹淺笑 提交于 2019-12-20 07:56:55

问题


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. I don't know how to narrow the search in order to get relevant information, or how to find what is wrong with the code I got.

ListActivity - the Adapter is initialized and a click listener is set here

public class ListActivity extends AppCompatActivity implements ListAdapter.ItemClickListener {

    ArrayList<Country> countriesList;
    ListAdapter adapter;
    RecyclerView rv;

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        adapter = new ListAdapter(context, this);
        adapter.setClickListener(this);

        //more irrelevant code
    }
}

    private Country getCurrentCountry(){
        return currentCountry;
    }

    public void setCurrentCountry(Country currentCountry){
        this.currentCountry = currentCountry;
    }

    @Override
    public void onItemClick(View view, int position) {

        FragmentTransaction ft;

        Bundle countryBundle = new Bundle();
        countryBundle.putParcelable("countriesList", getCurrentCountry());

        Fragment countryFragment = new CountryFragment();
        countryFragment.setArguments(countryBundle);
        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fragments_container, countryFragment);

        Log.d("Monitoring", getCurrentCountry().toString());

        ft.commit();
    }

The callback methods in the Adapter class

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.RowViewHolder> {
// Adapter class code
...
    class RowViewHolder extends RecyclerView.ViewHolder implements View.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);
        }

        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
            ListActivity.position = getAdapterPosition();
            final Country currentCountry = countriesList.get(getAdapterPosition());
            listActivity.setCurrentCountry(currentCountry);

        }
    }

    public void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
//more irrelevant code
}

Thanks!


回答1:


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

Change your interface parameter

 public interface ItemClickListener {
     void onItemClick(Country country);
 }

and you can pass the object you need

public void onClick(View view) {

     final Country currentCountry = countriesList.get(getAdapterPosition());

     mClickListener.onItemClick(currentCountry);
}

In your Activity, get your Country

 @Override
 public void onItemClick(Country country) {

      //  get country and do anything you want
 }

Hope this helps




回答2:


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

It should look like below.

class RowViewHolder extends RecyclerView.ViewHolder implements View.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);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
            ListActivity.position = getAdapterPosition();
            final Country currentCountry = countriesList.get(getAdapterPosition());
            listActivity.setCurrentCountry(currentCountry);

        }
    }



来源:https://stackoverflow.com/questions/58902529/callback-method-in-list-adapter-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!