2

リストビューを含むアクティビティがあります。(カスタムアダプターを利用します。adap1としましょう)

スイッチもありますが、

 <Switch
        android:id="@+id/mySwitch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="on/off" />

次のように初期化されます。

Switch switch1; //global
switch1 = (Switch) findViewById(R.id.mySwitch); //inside onCreate()
if (switch1 != null) 
{
    switch1.setOnCheckedChangeListener(this);
}
else
{
    Log.i(tag, "switch1 is null");
}

クリックすると、リストビューが変更されます(新しいものは、異なるレイアウトとビューを持つ別のカスタムアダプターを利用します。Adap2としましょう)。Adapt2 には、行のビューの 1 つとしてチェックボックスがあります。

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
      if(isChecked)
      {
            adapter1 = new Adap1 (this, al);
            lv.setAdapter(adapter1);        
      }
      else
      {     
            adapter2 = new Adap2 (this, al);
            lv.setAdapter(adapter2);
      }
}

そして、これを onCreate(); で呼び出します。

lv.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                if(switch1.isChecked())
                {               
                    Log.i(tag, "Switch is checked");
                }
                  else
                      {             
                        Log.i(tag, "Switch is not checked");
                      }

ただし、「スイッチがチェックされています」は表示されません。

それぞれが異なる XML を持つ 2 つの異なるカスタム アダプターを使用していて、コードが使用しているビューを認識していないためですか?

4

0 に答える 0