0

I'm new to Android. I have a ListActivity that include a ListView. The item in ListView is customized. The xml layout is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:textSize="22sp" ></TextView>

    <TextView 
        android:id="@+id/item2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingBottom="10dp" 
        android:paddingLeft="10dp" 
        android:paddingTop="5dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@null"
            android:onClick="favoriteOnClick"
            android:src="@drawable/favorite_logo" />


    </RelativeLayout>

</LinearLayout>

That have a ImageButton.
When the button was click. I want to retrieve which button was click(mean I need to retrieve which item of ListView).

Magic
  • 1,182
  • 2
  • 18
  • 28
  • u will get the id of the item displayed in listview ?isnt it – KOTIOS Aug 02 '13 at 08:47
  • If you want to use `favoriteOnClick` method to retrieve ImageButton click then in getView() of Custom Adapter just set Tag of ImageButton with position of List Item then retrieve it in `favoriteOnClick`using `getTag()`. – user370305 Aug 02 '13 at 08:51

2 Answers2

2

Well, you can handle it in your ListView Adapter.

In method : public View getView(final int position, View convertView, ViewGroup parent) you can deserialize your ImageButton, and then set the OnClickListener. getView() method give you the position

Skaard-Solo
  • 682
  • 6
  • 11
2

you can use tag attached to each view to retrieve the position in listview.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageButton ib = ...
    ib.setTag(new Integer(position));
    ib.setOnClickListener(myListener);
}

....

private OnClickListener myListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        int position = v.getTag(v.intValue());
    }
};
Zhenghong Wang
  • 2,117
  • 17
  • 19