Help for the onClick () event for a custom ListView item

I have a ListView whose rows are formatted by me. Each row has a combination of ImageView and TextView. I also implemented my own adapter and can draw every line.

Now I would like something like this -

  • The user clicks on the ImageView (nowhere in the line, but only this ImageView should respond to clicks)
  • I recognize the position of the row at which ImageView was clicked.

I tried many things for this and wanted my code to be as efficient as possible (in terms of redundancy). Currently, I can only capture the click event for this particular ImageView, but I cannot find out which row was clicked.

I have provided an attribute in Row XML, for example:

<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 line (maybe), but I'm not sure how to go any further from here. Can anybody help?

+4
source share
4 answers

Please refer to this,
I'm just writing code to give you an idea, Not in the right 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 this helped you

+13
source

Another option is to use the setTag() and getTag() methods for presentation. You install it in your getView as follows:

 imageView.setTag(new Integer(position)); 

Then in onClick() you can find the tag:

 Integer tag = v.getTag(); 

This will then be used to correlate the representation of the image with the position of the list item.

Note that this approach will give problems if the listview can lose elements from the middle, so that the position of the position changes over the life of the list.

+1
source

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

then you can handle the onclick event in your activity, for the context of activity here mActivity just pass it in the adapter constructor and drop it here into an activity of type Context myActivity mActivity = (MyActivity); in the adapter. thanks

0
source

This is similar to working in 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 } 
0
source

All Articles