события кнопки в динамическом ListAcitvity android - PullRequest
0 голосов
/ 03 марта 2012

Привет! Я создал динамический список. У меня есть SeekBar и ToggleButton для каждой строки в списке. Я пытался генерировать события для переключения и поиска за последнюю 1 неделю, но тщетно. Пожалуйста, помогите мне с этим.

Вот код XML со списком (smartunits.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/unitsLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/grid_gradient"
    android:orientation="vertical" >
    <ListView
        android:id="@android:id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Теперь код для каждой строки (listunits.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" >

    <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>

Вот код для ListActivity

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.

1 Ответ

0 голосов
/ 03 марта 2012

Я никогда не пробую это, но вы можете сделать что-то вроде

В вашем 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" />

И в вашем UnitsListActivity

напишите этот метод ..

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

Запустите этот код и дайте мне знать, что произойдет ..

...