2012-03-03 52 views
0

嗨,我已經生成了一個動態列表。我有SeekBar和一個ToggleButton列表中的每一行。我一直試圖爲切換生成事件,並從最後1周尋找,但徒勞無功。請幫助我。動態ListAcitvity中的按鈕事件android

下面是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

您必須實現自己的SimpleCursorAdapter並在bindView()方法中爲這些視圖設置偵聽器,同時也爲偵聽器設置了一種方法知道用戶執行了哪個「ToggleButton」和「SeekBar」。 – Luksprog 2012-03-03 07:23:21

+0

嘗試刪除'final ToggleButton中的'viewUnits' toggle =(ToggleButton)viewUnits.findViewById(R.id.toggle); '。另一個建議是建立一個敬酒,看看它是否正常。如果吐司工作,那麼'toggle.setBackgroundDrawable(getResources()。getDrawable(R.drawable.foo));'您正在使用的方法可能會出錯。當你說你無法處理'ToggleButton'事件時,請嘗試提及究竟發生了什麼。像崩潰或根本沒有結果。這種信息會有所幫助。 – Ghost 2012-03-03 07:35:05

回答

0

我從來沒有嘗試這個,但你可以做這樣的事情,

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

運行此代碼並讓我知道發生了什麼..

+0

我可能在這方面是錯誤的,但理論上我認爲它應該工作..! – user370305 2012-03-03 07:37:30

+0

@Ghost viewUnits是強制性的,因爲切換位於listunits而不是smartunits。該活動目前正在與smartunits合作。 – yama 2012-03-03 07:54:10

+0

@yama - 你嘗試過我建議的嗎? – user370305 2012-03-03 08:03:38