使用接口将字符串从适配器传递到片段 [英] Pass String from Adapter to Fragment using interface

查看:91
本文介绍了使用接口将字符串从适配器传递到片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是基于答案的问题.

我正在尝试将字符串从RecyclerView.Adapter传递给片段.

I'm trying to pass a string from my RecyclerView.Adapter to my fragment.

在我的适配器中,添加了以下内容:

In my adapter I added the following:

onItemClickListner onItemClickListner;

public void setOnItemClickListner(VideoAdapter.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner{
    void onClick(String str);//pass your object types.
}

一旦适配器中的项数少于一个(适配器为空),我就传递以下字符串:

I pass the following string once the amount of items in my adapter is less then one (adapter is empty):

onItemClickListner.onClick("TESTING");

然后,在我的片段中添加以下内容:

Then, in my fragment I add the following:

//I do the following after setting my adapter
videoAdapter.setOnItemClickListner(new VideoAdapter.onItemClickListner() {
        @Override
        public void onClick(String str) {
            Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
        }
    });

由于某种原因,字符串为空/空,并且崩溃指向onItemClickListner.onClick("TESTING");.

For some reason the string is empty/null and the crash points to onItemClickListner.onClick("TESTING");.

有人可以看看我可能做错了什么吗?

Can someone please have a look and see what I might be doing wrong?

我在OnMenuItemClickListener内部调用onItemClickListner.onClick("TESTING");,如下所示:

I call onItemClickListner.onClick("TESTING"); inside my OnMenuItemClickListener as shown below:

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {

                case R.id.delete:

                    onItemClickListner.onClick("TESTING");

                return true;

}

我没有提供完整的片段,因为我只是在onCreateView中添加了以上内容.

I did not provide my entire fragment because I just add the above inside onCreateView.

推荐答案

首先,感谢您的所有回答和评论.

Firstly, thank you for all the answers and comments.

我通过以下操作解决了这个问题.

I fixed this issue by doing the following.

在我的Adapter中,添加了以下内容:

In my Adapter I added the following:

private UpdateInterface listner;

public interface UpdateInterface {
    void recyclerviewOnUpdate(int amount);
}

public VideoAdapter(Context context, ArrayList<PathModel> thumbPathList, ArrayList<PathModel> videoPathList, UpdateInterface listner) {
    this.context = context;
    this.thumbPathList = thumbPathList;
    this.videoPathList = videoPathList;
    //I added this
    this.listner=listner;
}

我以与在问题中相同的方式调用该接口:

I call the interface the same way as I did in my question:

//Passing the amount of items in adapter
listner.recyclerviewOnUpdate(getItemCount());

这与我之前所做的非常相似,但是现在我在片段中实现了这样的接口:

It's very similar to what I've done above but now I implement the interface in my fragment like this:

public class VideoFragment extends Fragment implements VideoAdapter.UpdateInterface

现在我可以使用一种方法,如下所示:

and now I have a method to work with, shown below:

@Override
public void recyclerviewOnUpdate(int amount) {
    //Now I can get the int and check if the adapter is empty after removing a item
    if (amount<1) {
        txtEmptyAdapter.setVisibility(View.VISIBLE);
    }
}

这篇关于使用接口将字符串从适配器传递到片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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