适配器回调 [英] Callback from Adapter

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

问题描述

所以我有一个自定义列表视图.这就像 Instagram 布局,每个列表项中有 1 张图像和一堆按钮.所以问题来了:

我想实现分享按钮.为此,我尝试创建从适配器到活动的回调.但它似乎没有用.这是我到目前为止所拥有的(我剪掉了不相关的部分):

<块引用>

主活动

公共类 MainActivity 扩展 ActionBarActivity 实现 ListAdapter.OnShareClickedListener{ListView main_list;列表<字符串>网址列表;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);main_list = (ListView) findViewById(R.id.listView);ListAdapter nListAdapter = new ListAdapter(context, url_list);main_list.setAdapter(nListAdapter);}@覆盖公共无效 ShareClicked(字符串网址){Log.e("测试",url);}}

<块引用>

列表适配器

public class ListAdapter extends BaseAdapter 实现 View.OnClickListener {OnShareClickedListener mCallback;上下文上下文;公共静态列表网址列表;公共列表适配器(上下文c,列表<字符串>列表){this.context = c;url_list = 列表;}公共接口 OnShareClickedListener {public void ShareClicked(String url);}@覆盖public void onClick(View v) {mCallback.ShareClicked("分享这段文字.");}}}

<块引用>

错误日志:

尝试在空对象引用上调用接口方法void com.packagename.ListAdapter$OnShareClickedListener.ShareClicked(java.lang.String)"

解决方案

您需要告诉适配器使用 OnShareClickedListener() 的哪个实现.现在在您的适配器中,字段 mCallback 从未分配给,或者您需要在您的适配器中有一个 setOnSharedClickedListener() 方法,然后您从 mainActivity 调用该方法并使用主要活动的实现设置它,或者您需要接受构造函数.

我的建议是使用 setter 而不是构造函数.所以你需要做的是这个.

您的列表适配器

public class ListAdapter extends BaseAdapter 实现 View.OnClickListener {OnShareClickedListener mCallback;上下文上下文;公共静态列表网址列表;公共列表适配器(上下文c,列表<字符串>列表){this.context = c;url_list = 列表;}public void setOnShareClickedListener(OnShareClickedListener mCallback) {this.mCallback = mCallback;}公共接口 OnShareClickedListener {public void ShareClicked(String url);}@覆盖public void onClick(View v) {mCallback.ShareClicked("分享这段文字.");}}

您的主要活动

公共类 MainActivity 扩展 ActionBarActivity 实现 ListAdapter.OnShareClickedListener{ListView main_list;列表<字符串>网址列表;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);main_list = (ListView) findViewById(R.id.listView);ListAdapter nListAdapter = new ListAdapter(this, url_list);nListAdapter.setOnShareClickedListener(this);main_list.setAdapter(nListAdapter);}@覆盖公共无效 ShareClicked(字符串网址){Log.e("测试", url);}}

So I have a custom listview. It's like Instagram layout with 1 image and bunch of buttons in each list items. So here's the problem:

I want to implement the share button. To do this, I tried to create a callback from adapter to activity. But it didn't seem to work. Here is what I have so far(I cropped out the unrelated parts):

MainActivity

public class MainActivity extends ActionBarActivity implements ListAdapter.OnShareClickedListener{

    ListView main_list;
    List<String> url_list; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_list = (ListView) findViewById(R.id.listView);
        ListAdapter nListAdapter = new ListAdapter(context, url_list);
        main_list.setAdapter(nListAdapter);
    }

    @Override
    public void ShareClicked(String url) {
        Log.e("Test",url);
    }

}

ListAdapter

public class ListAdapter extends BaseAdapter implements View.OnClickListener {

    OnShareClickedListener mCallback;
    Context context;
    public static List<String> url_list;

    public ListAdapter(Context c, List<String> list) {
        this.context = c;
        url_list = list;
    }

    public interface OnShareClickedListener {
        public void ShareClicked(String url);
    }


    @Override
    public void onClick(View v) {
                mCallback.ShareClicked("Share this text.");
        }
    }

}

Error Log:

Attempt to invoke interface method 'void com.packagename.ListAdapter$OnShareClickedListener.ShareClicked(java.lang.String)' on a null object reference

解决方案

You need to tell the adapter which implementation of the OnShareClickedListener() to use. Right now in your adapter the field mCallback is never assigned to, either you need to have a setOnSharedClickedListener() method in your adapter which you then call from your mainActivity and set it with the main activity's implementation or you need to take in the constructor.

My suggestion would be to use a setter instead of constructor. So what you need to do is this.

Your ListAdapter

public class ListAdapter extends BaseAdapter implements View.OnClickListener {

    OnShareClickedListener mCallback;
    Context context;
    public static List<String> url_list;

    public ListAdapter(Context c, List<String> list) {
        this.context = c;
        url_list = list;
    }

    public void setOnShareClickedListener(OnShareClickedListener mCallback) {
        this.mCallback = mCallback;
    }

    public interface OnShareClickedListener {
        public void ShareClicked(String url);
    }


    @Override
    public void onClick(View v) {
        mCallback.ShareClicked("Share this text.");
    }
}

Your MainActivty

public class MainActivity extends ActionBarActivity implements ListAdapter.OnShareClickedListener{

    ListView main_list;
    List<String> url_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_list = (ListView) findViewById(R.id.listView);
        ListAdapter nListAdapter = new ListAdapter(this, url_list);
        nListAdapter.setOnShareClickedListener(this);
        main_list.setAdapter(nListAdapter);
    }

    @Override
    public void ShareClicked(String url) {
        Log.e("Test", url);
    }

}

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

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