如何在适配器启动活动? [英] How to start Activity in adapter?

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

问题描述

我有我的定制适配器ListActivity。并在每个视图,它可能有一些按钮,在我需要实现OnClickListener。我需要实现OnClickListener的适配器。但是,我不知道该怎么称呼像startActivity()函数或的setResult()。由于适配器犯规扩展到活动。

I have a ListActivity with my customized adapter. and inside each of the view, it may have some buttons, in which i need to implement OnClickListener. I need to implement the OnClickListener in the adapter. However, I dont know how to call the function like startActivity() or setResult(). since the adapter doesnt extend to Activity.

那么,什么是解决这个问题的最好方法是什么?谢谢

so what is the best way to solve this problem ? thanks

推荐答案

只是通过在目前情况下的适配器的构造,并存储为一个字段。然后onClick的内部,你可以使用该上下文来调用startActivity()。

Just pass in the current Context to the Adapter constructor and store it as a field. Then inside the onClick you can use that context to call startActivity().

伪code

public class MyAdapter extends Adapter {
     private Context context;

     public MyAdapter(Context context) {
          this.context = context;     
     }

     public View getView(...){
         View v;
         v.setOnClickListener(new OnClickListener() {
             void onClick() {
                 context.startActivity(...);
             }
         });
     }
}

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

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