4

リスト ビューの行のカスタム レイアウトがあります。

<?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:background="@drawable/listSelector"
    android:orientation="horizontal">

    <LinearLayout
            android:id="@+id/checkboxSelection1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dip">

        <CheckBox android:id="@+id/checkbox1" />

        </LinearLayout>

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/checkbox1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

表示する適切なデータを取得するためのアダプターもあります。それがします。UI の観点からは、そうしたいように見えます。

ただし、チェックボックスをクリックしても何も起こりません。選択したアイテムのリストをバックエンド (理想的にはアクティビティ クラス) に保存したいと考えています。

アクティビティ クラスの onCreate には、次のコードがあります。

listView.setAdapter(adapter);

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

// Click event for single list row
listView.setOnItemClickListener(new OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        int i = 1;
    }
});

私は int 1 = 1; を持っています。ブレークポイントを追加して、ヒットするかどうかを確認できるように、そこに行を追加しました。そうではありません。チェックボックスなどではなくリストビューの行に接続されているなど、何か間違っていると確信していますが、イベントをチェックボックスに接続する方法がわかりません。

誰かが私を正しい方向に向けることができれば、私はそれを感謝します.

ありがとう

編集:明確にするために

私はこれをアダプターに持っています:

taskChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{

    public void onCheckedChanged(CompoundButton arg0, boolean arg1) 
    {
        // TODO Auto-generated method stub
        int i = 1;
    }
});

そして、そのブレークポイントはヒットします。そのため、チェックボックスが選択されているか選択されていないときに、アダプターだけでなく、アクティビティでイベントが発生する方法を見つけようとしています。

4

2 に答える 2

1

私はそれを考え出した。

アダプターにこれを追加しました: _activity は、呼び出し元のアクティビティからコンストラクターに渡されるアクティビティです。myObj は、getView 内の位置とアダプターの構築時に渡されたデータに基づいて、コードの前半で最終として宣言されます。

taskChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean checked) 
            {
                // Cast it so we can access the public functions
                MyActivity myActivity = (MyActivity) _activity;

                if (checked) // true if the checkbox is checked, false if unchecked
                {
                    myActivity.checkboxSelected(myObj);
                }
            }
        });

そして、アクティビティでこれを追加しました:

public void checkboxSelected(MyObj myObj)
{
    // Do stuff with myObj here
}

うまくいけば、これは誰かを助けます。

于 2013-11-01T06:45:16.957 に答える