5

I have a ListView whose rows are formatted by me. Each row has a mix of ImageView and TextView. I have also implemented my own adapter and am able to draw each row through it.

Now, I would want something like this-

  • User clicks on an ImageView (not anywhere else on the row, but only this ImageView should respond to clicks)
  • I get to know the position of the row whose ImageView was clicked.

I have tried many things for this and have wanted my code to be as efficient as possible (in terms of overkill). Currently i can capture the click event on that particular ImageView only, but I can't know which row was clicked.

I have provided an attribute in the Row XML like this-

<ImageView android:id="@+id/user_image"
    android:padding="5dip" 
    android:layout_height="60dip" 
    android:layout_width="60dip"
    android:clickable="true"
    android:onClick="uImgClickHandler"/> 

And in my code, I have a method like this:

public void uImgClickHandler(View v){
  Log.d("IMG CLICKED", ""+v.getId());
  LinearLayout parentRow = (LinearLayout)v.getParent();

 }

I can get the parent row (perhaps) but am not sure how to go further from here. Can someone please help?

2
  • What exactly would you like to achieve? Do you want to set some properties on the other row widgets? Commented Nov 15, 2010 at 6:42
  • I wanted the user to click on imageview only, not anywhere else on the row, and based on the imageview clicked, i wanted to know the position of the row, and then look-up in the data-source at that position, get some info and start another activity. It is very much solved by the following answer by the way. Commented Nov 15, 2010 at 7:46

4 Answers 4

14

Please refer this,
Me just writing the code to give you idea, Not in correct format

 class youaddaper extends BaseAdapter{

   public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflate = LayoutInflater.from(context);
        View v = inflate.inflate(id, parent, false);

      ImageView imageview = (ImageView) v.findViewById(R.id.imageView);
        imageview.setOnClickListener(new imageViewClickListener(position));
      //you can pass what ever to this class you want,
      //i mean, you can use array(postion) as per the logic you need to implement 
   }
   class imageViewClickListener implements OnClickListener {
   int position;
    public imageViewClickListener( int pos)
        {
            this.position = pos;
        }

    public void onClick(View v) {
      {// you can write the code what happens for the that click and 
       // you will get the selected row index in position
     }
}

}

Hope it helped you

Sign up to request clarification or add additional context in comments.

3 Comments

Good. it really did help. I am going to try it out.
It worked. But might add some overhead since it separate objects for each imageview. will optimize it later. thanks! :)
This also worked for me. I'm interested in your comment about the creation of separate objects and what would be a better way to do it. The few examples I have seen always creates the listener objects in the inside.
1

Another option is to use the methods setTag() and getTag() of the view. You set it in your getView like this:

imageView.setTag(new Integer(position));

Then in the onClick() you can find the tag by:

Integer tag = v.getTag();

This will then be used to correlate the image view to the position of the listview item.

Note that this approach will give problems if the listview can lose items from the middle, so that the item positions change during the lifetime of the listview.

Comments

0

you can simply do like this: in the getview method of our adapter Button btn1 = (Button) convertView.findViewById(R.id.btn1); btn1.setOnClickListener(mActivity);

further you can handle the onclick event in your activity,, for the context of the activity here mActivity just pass the this in the constructer of the adapter and cast it here into the activity like MyActivity mActivity=(MyActivity)context; in the adapter. thanx

1 Comment

That's a valid option, but when this button is clicked and the control is passed to my central method residing in the activity, it wouldn't have enough knowledge about which row this button was on, that makes it difficult.
0

This appears to work in a ListActivity whose item layout contains an ImageView with android:onClick="editImage":

public void editImage(View v) {
        int[] loc = new int[2];
        v.getLocationInWindow(loc);
        int pos = getListView().pointToPosition(loc[0], loc[1]);
        Cursor c = (Cursor) adapter.getItem(pos);
        // c now points at the data row corresponding to the clicked row
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.