Android:自定义Listadapter中列表项内的ImageView上的OnClickListener

时间:2013-07-15 13:48:02

标签: android listview onclicklistener android-cursoradapter

我有自定义Listview和Cursoradapter。这是我的列表的样子: my list http://kepfeltoltes.hu/130715/device-2013-07-15-152336_www.kepfeltoltes.hu_.png

当我点击列表项时,我想播放录音,当我点击信息按钮时,我想开始一个新的活动。 我在ImageView上设置了一个onClickListener(名为detailsIcon),并在其上设置了一个带有记录ID的标签。我以后无法检索标签的问题,其他奇怪的是所有的ImageView似乎都有相同的ID。

当系统填充列表时,这是日志文件:

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

当我在imageView上进行clc时,它会因为nullpointer异常而崩溃。 id =(Integer)v.getTag()返回null。我该如何解决这个问题?

这是我的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() + ") ");
            }
        });
    }

这是我的列表项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>

1 个答案:

答案 0 :(得分:1)

将OnClickListener更改为以下内容:

    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 + ") ");
        }
    });

当你调用detailsIcon.setTag(R.id.recording_id,rec.getId());您正在将标记的键设置为R.id.recording_id。您必须使用此密钥从视图中检索标记。如果您尝试设置视图的ID,可以调用detailsIcon.setId();。