1

我有一个带有光标适配器的自定义列表视图。这就是我的列表的样子:( 来源:kepfeltoltes.hu我的列表

当我点击一个列表项时,我想播放录音,当我点击信息按钮时,我想开始一个新的活动。我在 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 上单击时,它会因空指针异常而崩溃。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>
4

1 回答 1

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();。

于 2013-07-15T14:01:21.977 回答