Android: OnClickListener on an ImageView inside a list item in a custom Listadapter

1

I have custom Listview with a Cursoradapter. This is how my list looks like: my list
(source: kepfeltoltes.hu)

When I click on a list item, I want to play the recording and when I click on the info button I want to start a new Activity. I set an onClickListener on the ImageView (called detailsIcon) and set a tag on it with the ID of the recording. The problem that I cannot retrive the tag later, other strange thing is that all the ImageView seems to have the same ID.

This is the log file, when the system populates the list:

07-15 14:33:28.238: I/System.out(27753): Setting icon (ID: 2131296298) on view (ID: -1) tag to:8
07-15 14:33:28.328: I/System.out(27753): Setting icon (ID: 2131296298) on view (ID: -1) tag to:7
07-15 14:33:28.368: I/System.out(27753): Setting icon (ID: 2131296298) on view (ID: -1) tag to:6
07-15 14:33:28.403: I/System.out(27753): Setting icon (ID: 2131296298) on view (ID: -1) tag to:5
07-15 14:33:28.418: I/System.out(27753): Setting icon (ID: 2131296298) on view (ID: -1) tag to:4
07-15 14:33:28.433: I/System.out(27753): Setting icon (ID: 2131296298) on view (ID: -1) tag to:3
07-15 14:33:28.453: I/System.out(27753): Setting icon (ID: 2131296298) on view (ID: -1) tag to:2
07-15 14:33:40.878: I/System.out(27753): Setting icon (ID: 2131296298) on view (ID: -1) tag to:1
07-15 14:33:41.238: I/System.out(27753): Setting icon (ID: 2131296298) on view (ID: -1) tag to:8

When I clcik on the imageView, it crashes with a nullpointer exception. The id = (Integer) v.getTag() gives back null. How can I solve this problem?

This is the interesting part of my CursorAdapter:

@Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView title = (TextView) view.findViewById(R.id.title);
        TextView date = (TextView) view.findViewById(R.id.date);
        ImageView btn_play_pause = (ImageView) view
                .findViewById(R.id.btn_play_pause);
        TextView duration = (TextView) view.findViewById(R.id.length);
        ImageView detailsIcon = (ImageView) view
                .findViewById(R.id.icon_details);

        rec = new RecordedObject(cursor);

        view.setTag(R.id.recording_id, rec.getId());

        ////////////////////////////////////////////////////////
        //here is the part, that is not working properly
        detailsIcon.setTag(R.id.recording_id, rec.getId());
        System.out.println("Setting icon (ID: " + detailsIcon.getId()
                + ") on view (ID: " + view.getId() + ") tag to:" + rec.getId());
        ////////////////////////////////////////////////////////

        title.setText(rec.getTitle());
        date.setText(rec.getDateReadable());

        duration.setText(MyDateFormatter.formatDuration(rec.getLength()));

        if (cursor.getPosition() == selectedRecording) {
            view.setBackgroundDrawable(context.getResources().getDrawable(
                    R.drawable.list_item_selected));
            title.setTextColor(0xFFFFFFFF);
            date.setTextColor(0xFFFFFFFF);
            duration.setTextColor(0xFFFFFFFF);
            btn_play_pause.setVisibility(View.VISIBLE);

        } else {
            view.setBackgroundDrawable(context.getResources().getDrawable(
                    R.drawable.list_item_normal));
            title.setTextColor(0xFF000000);
            date.setTextColor(0xFF000000);
            duration.setTextColor(0xFF000000);
            btn_play_pause.setVisibility(View.GONE);

        }

        detailsIcon.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int id;
                id = (Integer) v.getTag();
                System.out.println("View (ID: " + v.getId() + ") ");
            }
        });
    }

This is my list item xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip"
    android:descendantFocusability="blocksDescendants" >

    <!-- ListRow Left side: play button -->

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dip"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/icon_details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_info_details" />

        <ImageView
            android:id="@+id/btn_play_pause"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:contentDescription="@string/play"
            android:src="@android:drawable/ic_media_play"
            android:visibility="gone" />
    </LinearLayout>

    <!-- File name -->

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/thumbnail"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/thumbnail"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/title_of_recording"
            android:textSize="17dip"
            android:textStyle="bold"
            android:typeface="sans" />

        <TextView
            android:id="@+id/length"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="length of recording" />

    </LinearLayout>

    <!-- Date -->


    <!-- Rightend Duration -->

    <TextView
        android:id="@+id/length_old"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dip"
        android:gravity="right"
        android:textSize="9dip"
        android:textStyle="normal"
        android:visibility="gone" />

    <!-- Rightend Arrow -->

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"

        android:text="@string/date_of_recording"
        android:textSize="12dip" />

</RelativeLayout>
android
listview
onclicklistener
android-cursoradapter
asked on Stack Overflow Jul 15, 2013 by czadam • edited Sep 5, 2019 by Glorfindel

1 Answer

1

Change your OnClickListener to the following:

    detailsIcon.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            int id;
            id = (Integer) v.getTag(R.id.recording_id);
            System.out.println("View (ID: " + id + ") ");
        }
    });

When you call detailsIcon.setTag(R.id.recording_id, rec.getId()); you are setting the key for the tag to R.id.recording_id. You must use this key to retrieve the tag from the view. If you were trying to set the id of the view you can call detailsIcon.setId();.

answered on Stack Overflow Jul 15, 2013 by Bobbake4

User contributions licensed under CC BY-SA 3.0