如何通过一个列表视图的图像按钮,点击开始一个活动? [英] How to start an activity by click of image button of a List view?

查看:125
本文介绍了如何通过一个列表视图的图像按钮,点击开始一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表有一个形象按钮中的每个项目,当我一个形象,我要开始另一个活动意图

以下是我的code

我的问题是我无法从自定义适配器调用 startActivity()! !

在自定义适配器的 getView()

  holder.profilePicture.setOnClickListener(新OnClickListener()
       {
           公共无效的onClick(视图v)
           {
               Log.d(OnImageButton,点击);
               意图变焦=新意图(AllProfile.getAppContext(),ImageZoom.class);
               INT imageID = holder.profilePicture.getId();
               zoom.putExtra(ImageId,imageID);
               startActivity(缩放); //此行引发错误!
                           }
       });


解决方案

您应该通过上下文到你的自定义适配器,当你创建它:

 公共类MyAdapter延伸BaseAdapter {
    私人语境mContext;    公共MyAdapter(上下文CTX){
        mContext = CTX;
    }    ...
}

而在getView使用这种背景下启动的活动:

  holder.profilePicture.setOnClickListener(新OnClickListener()
   {
       公共无效的onClick(视图v)
       {
           Log.d(OnImageButton,点击);
           意图变焦=新意图(mContext,ImageZoom.class);
           INT imageID = holder.profilePicture.getId();
           zoom.putExtra(ImageId,imageID);
           mContext.startActivity(缩放); //此行引发错误!
                       }
   });

当你创建你的活动适配器你应该通过本作为参数:

  mAdapter =新MyAdapter(本);

i have a list there is a image button in each item when i click on a image i have to start another Activity with intent

following are my code

my problem is i couldn't call startActivity() from custom adapter! !

in custom adapter's getView()

  holder.profilePicture.setOnClickListener(new OnClickListener() 
       { 
           public void onClick(View v) 
           {
               Log.d("OnImageButton","Clicked");
               Intent zoom=new Intent(AllProfile.getAppContext(), ImageZoom.class);
               int imageID=holder.profilePicture.getId();
               zoom.putExtra("ImageId", imageID);
               startActivity(zoom)  ; //This line raises error !      
                           }


       });

解决方案

You should pass your context to your custom adapter when you create it:

public class MyAdapter extends BaseAdapter {
    private Context mContext;

    public MyAdapter (Context ctx) {
        mContext = ctx;
    }

    ...
}

And in getView use that context to start the Activity:

 holder.profilePicture.setOnClickListener(new OnClickListener() 
   { 
       public void onClick(View v) 
       {
           Log.d("OnImageButton","Clicked");
           Intent zoom=new Intent(mContext, ImageZoom.class);
           int imageID=holder.profilePicture.getId();
           zoom.putExtra("ImageId", imageID);
           mContext.startActivity(zoom)  ; //This line raises error !      
                       }


   });

When you create the Adapter in your Activity you should pass "this" as a parameter:

 mAdapter = new MyAdapter(this);

这篇关于如何通过一个列表视图的图像按钮,点击开始一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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