2

テキストとチェックボックスを含むリストビューがあります。チェックボックスをクリックするとチェックが変更されますが、変更されたチェックまたはクリックをリッスンしようとすると、リスナーがトリガーされません。

アクティビティにリストビューがあります (ListActivity ではありません)

schedule.xml レイアウト:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/schedulerLayout">
<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:paddingBottom="10dp">
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/currentHours"
                android:textSize="50dp"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="50dp"
                android:text=":"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/currentMinutes"
                android:textSize="50dp"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="vertical">
        <!--vertical filler for am/pm display -->
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ampm"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:orientation="vertical">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/currentDayOfWeek"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/currentDate"/>
    </LinearLayout>
</LinearLayout>

<Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scheduleButton"
        android:text="@string/newSchedule"/>
<View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#FFFFFFFF"/>

<ListView
        android:id="@+id/scheduler_list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
</LinearLayout>

スケジュール行.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:layout_alignParentLeft="true"
    android:layout_alignParentTop="true">
<TextView
        android:id="@+id/scheduleListTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:text="Title"/>

<TextView
        android:id="@+id/scheduleListDays"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/scheduleListTitle"
        android:text="Days"/>
<TextView
        android:id="@+id/scheduleListMonths"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/scheduleListDays"
        android:text="Months"/>
<View
        android:id="@+id/schedulerSeparator"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#FFFFFFFF"
        android:layout_below="@id/scheduleListMonths"/>
<CheckBox
        android:id="@+id/scheduleCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="true"
        android:layout_above="@id/schedulerSeparator"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>
<TextView
        android:id="@+id/scheduleListTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/scheduleCheckBox"
        android:text="Time"/>
</RelativeLayout>

次のようにデータを入力しています。

SimpleAdapter adapter = new SimpleAdapter(this,
            data,
            R.layout.schedulerow,
            new String[] {"Title","Time","Day", "Month"},
            new int[] { R.id.scheduleListTitle, R.id.scheduleListTime,R.id.scheduleListDays, R.id.scheduleListMonths});

listView.setAdapter(adapter);

チェックボックスにリスナーを追加しようとしても、何もしません。助言がありますか?私はたくさんのサンプル コードと同様の質問に目を通しましたが、どれも機能していないようです。

4

1 に答える 1

2

解決策の 1 つは、リスト アイテムのクリック リスナーを忘れて、チェックボックス以外の残りのアイテムに別のリスナーを使用することです。アイテム レイアウト xml で、他のビューを単一のレイアウトに配置し、これにクリック リスナーを設定し、チェック リスナーを設定します。いつものようにチェックボックスに。どちらも問題なく動作します。リスト項目のクリック リスナーを使用しないでください。

class MySimpleAdapter extends SimpleAdapter {
 ... 
   @Override
   void getView(.....) {
       View view = super.getView(.....);
       view.findViewById(R.id.checkboxid).setOnCheckChangedListner(checklistener);
       view.findViewById(R.id.restOfitemview).setOnClickListner(clickListner);
       return view;
    }
}

2 つのリスナーを定義し、上記のように設定します。

編集:最初MySimpleAdapterにcheckchangelistenerのみを設定してメソッド(チェックボックスにフォーカスをブロックする)を試してください。それが機能しない場合は、アイテムレイアウトを分割する上記のソリューションを使用してください。

于 2012-11-21T16:39:32.570 に答える