Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android - how to make an imageview clickable in an listview

Hi i have created a basic listview and added a textview and imageview in it.

<ImageView
    android:id="@+id/icon"
    android:layout_width="50px"
    android:paddingLeft="2px"
    android:paddingRight="2px"
    android:paddingTop="2px"
    android:layout_height="wrap_content"
    android:layout_alignParentRight = "true"
    android:src="@drawable/call"
/>

I need to make imageview clickable so that if some clicks on it an action will happen like an new activity is opened.Can someone help me how to do this.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to implement your own cursor adapter, and in that you have to override the getView method and then set the onclick listener to your image:

public class SMSimpleCursorAdapter extends SimpleCursorAdapter{

    Context context;
    Activity activity;
    public SMSimpleCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.context=context;
        this.activity=(Activity) context;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View view = super.getView(position, convertView, parent);
        long id=getItemId(position);
        ImageView image= (ImageView)view.findViewById(R.id.icon);
        image.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {

            }
        });


    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...