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)。Adap2 有一个复选框作为其在行中的视图之一。

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

但是,永远不会显示“已检查开关”。

是不是因为我使用了 2 个不同的自定义适配器,每个适配器都有不同的 XML,并且代码不知道我使用的是哪个视图?

4

0 回答 0