列表适配器中的回调方法不起作用 [英] Callback method in list Adapter doesn't work

查看:73
本文介绍了列表适配器中的回调方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了在上一个问题中被指导使用的回调方法.它似乎不起作用.不调用onClick()方法.回调方法似乎是一个非常广泛的概念.我不知道如何缩小搜索范围以获取相关信息,或者如何找到我得到的代码有什么问题.

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 -适配器已初始化,并在此处设置了点击侦听器

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();
    }

Adapter类中的回调方法

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
}

谢谢!

推荐答案

您无需传递视图和位置,只需传递所需的对象即可.

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

更改界面参数

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

您可以传递所需的对象

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
 }

希望这会有所帮助

这篇关于列表适配器中的回调方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆