动态Listacity android中的按钮事件

动态Listacity android中的按钮事件,android,listactivity,togglebutton,Android,Listactivity,Togglebutton,嗨,我已经生成了一个动态列表。对于列表中的每一行,我都有一个SeekBar和一个ToggleButton。在过去的一周里,我一直在尝试为切换和搜索生成事件,但没有成功。请帮帮我 以下是带有列表的XML代码(smartunits.XML) 现在我无法处理ToggleButton事件。我从未尝试过,但您可以执行以下操作: 在您的listunits.xml中 <ToggleButton android:id="@+id/toggle" android:layout_width="

嗨,我已经生成了一个动态列表。对于列表中的每一行,我都有一个SeekBar和一个ToggleButton。在过去的一周里,我一直在尝试为切换和搜索生成事件,但没有成功。请帮帮我

以下是带有列表的XML代码(smartunits.XML)


现在我无法处理ToggleButton事件。

我从未尝试过,但您可以执行以下操作:

在您的listunits.xml中

<ToggleButton
    android:id="@+id/toggle"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="5dp"
    android:textOff=""
    android:textOn=""
    android:onClick="toggleButtonClick" />

运行此代码并让我知道发生了什么。

您必须在
bindView()中实现自己的
SimpleCursorAdapter
method为这些视图设置监听器,但也为监听器设置一种方式,让其知道用户对哪个
ToggleButton
和SeekBar`进行了操作。尝试在
final ToggleButton toggle=(ToggleButton)viewUnits中删除
viewUnits
。另一个建议是干杯,看看它是否管用。如果toast有效,那么
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.foo))可能有问题您正在使用的方法。当你说你无法处理
ToggleButton
事件时,试着提及到底发生了什么。比如撞车或者根本没有结果。一些这样的信息可能会有帮助。我可能在这方面错了,但理论上我认为它应该有效@Ghost viewUnits是必需的,因为切换在listunits中,而不是smartunits中。“活动”当前正在使用smartunits。我刚刚试用过它,但它不起作用。我也尝试过toast(用于toggleButtonClick),但它没有显示任何文本。只需将调试器指向toggleButtonClick方法,并检查此方法是否执行。我无法跟踪调试器的问题。
<?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" >

    <TextView
        android:id="@+id/unitName"
        android:layout_width="40px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="20px"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/unitCodeId"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:visibility="invisible" />

    <SeekBar
        android:id="@+id/unitsseek"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <ToggleButton
        android:id="@+id/toggle"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="5dp"
        android:background="@drawable/off_grey"
        android:textOff=""
        android:textOn="" />

</LinearLayout>
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SimpleCursorAdapter;
import android.widget.ToggleButton;

public class UnitsListActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        // put the bottom code here

        setContentView(R.layout.smartunits);

        Bundle extras = getIntent().getExtras();

        final AutomationDBAccessor db = new AutomationDBAccessor(this);
        Cursor cUnits = db.getUnits(extras.getInt("roomFloorId"), extras.getInt("roomId"));

        String[] from = new String[] { AutomationDBAccessor.colUnitName, AutomationDBAccessor.colUnitCodeID };
        int[] to = new int[] { R.id.unitName, R.id.unitCodeId };
        SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.listunits, cUnits, from, to);

        this.setListAdapter(sca);

        View viewUnits = LayoutInflater.from(getBaseContext()).inflate(R.layout.listunits, null);
        final ToggleButton toggle = (ToggleButton) viewUnits.findViewById(R.id.toggle);

        toggle.setOnClickListener(new OnClickListener() {

            public void onClick(View v) { // TODO Auto-generated method stub

                if (toggle.isChecked()) {

                    toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.on));
                } else {
                    toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.off_grey));

                }

            }
        });
    }
}
<ToggleButton
    android:id="@+id/toggle"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="5dp"
    android:textOff=""
    android:textOn=""
    android:onClick="toggleButtonClick" />
public void toggleButtonClick(View view)
{   
    ToggleButton toggle = (ToggleButton) view;

    if (toggle.isChecked()) {
        toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.on));
    } else {
        toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.off_grey));
    }
}