Welcome to OStack Knowledge Sharing 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 Answer

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) 
            {

            }
        });


    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...