2012-09-01 128 views
1

我正在寫一個應用程序,如列表視圖與切換按鈕。 這是我的源代碼。列表視圖與切換按鈕

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.main,R.id.textView1,getResources().getStringArray(R.array.Strings)); 
    setListAdapter(adapter); 

    } 

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
    String item = (String) getListAdapter().getItem(position); 
    Toast.makeText(this, item+" selected", Toast.LENGTH_LONG).show(); 
} 

而且我從進口字符串資源列表,我的sring.xml是 MyListViewWithToggleButton 世界,你好! 設置 MyListViewWithToggle ON/OFF

<string-array name="Strings"> 

    <item >Relay1</item> 
    <item >Relay2</item> 
    <item >Relay3</item> 
    <item >Relay4</item> 
    <item >Relay5</item> 
    <item >Relay6</item> 
    <item >Relay7</item> 
    <item >Relay8</item> 
    </string-array> 
    </resources> 

而在畝main.xml中我使用一個文本視圖和切換按鈕

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_toLeftOf="@+id/toggleButton1" 
    android:textSize="20dp" > 
    </TextView> 


    <ToggleButton 
    android:id="@+id/toggleButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="10dp" 
     android:text="@string/On_Off" > 

     </ToggleButton> 


     </RelativeLayout> 

當我點擊切換按鈕它示出開和關。 但是,當我點擊列表,我沒有得到任何答覆。

對於我的應用程序,我需要發送一些數據,同時按下切換按鈕的按鈕條件。

回答

0
ListView menuListView; 

@Override 
protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.menuitem); 
    menuListView = (ListView) findViewById(R.id.menu); 
    TextView cartItem = (TextView) findViewById(R.id.cartItem); 
    Bundle b = getIntent().getExtras(); 
    int catId = b.getInt("categoryId");  
    List<MenuItem> menuItems = getMenuItemsByCatId(catId); 

    MenuLazyAdapter menuLazyAdapter = new MenuLazyAdapter(this, menuItems); 
    // Set the ListView adapter 

    menuListView.setAdapter(menuLazyAdapter); 
    menuListView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      Toast.makeText(getApplicationContext(), "Clicked on :: "+ position, Toast.LENGTH_SHORT).show(); 

     } 
    }); 


} 
private List<MenuItem> getMenuItemsByCatId(int catId) { 
    RomDbAdapter dbAdapter = new RomDbAdapter(MenuListView.this); 
    dbAdapter.open(); 

    List<MenuItem> items = dbAdapter.getAllMenuByCatId(catId); 
    dbAdapter.close(); 

    return items; 
} 

menuitem.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" 
    android:orientation="vertical" > 



    <LinearLayout 
     android:id="@+id/cart" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:layout_marginRight="5dip"  
     android:padding="3dip" > 
     <TextView 
     android:id="@+id/cartItem" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Menu drop here" 
     android:gravity="right" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    </LinearLayout> 
    <ListView 
     android:id="@+id/menu" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:cacheColorHint="#cccccc" 
     android:scrollbars="vertical" 
     android:smoothScrollbar="true" > 
    </ListView> 

</LinearLayout> 

item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="10dip" > 

    <!-- ListRow Left sied Thumbnail image --> 

    <LinearLayout 
     android:id="@+id/thumbnail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="5dip" 
     android:background="@drawable/image_bg" 
     android:padding="3dip" > 

     <ImageView 
      android:id="@+id/item_image" 
      android:layout_width="50dip" 
      android:layout_height="50dip" /> 
    </LinearLayout> 

    <!-- Title Of item --> 

    <TextView 
     android:id="@+id/itemName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/thumbnail" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:text="Item Name"   
     android:textSize="15dip" 
     android:textStyle="bold" 
     android:typeface="sans" /> 

     <!-- Item description --> 



    <TextView 
     android:id="@+id/ItemDesc" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/itemName" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:text=" Fresh salad with lentils leafs" 
     android:textSize="10dip" /> 

    <TextView 
     android:id="@+id/itemPrice" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/ItemDesc" 
     android:layout_below="@+id/ItemDesc" 
     android:text="$10.00" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 
+0

海感謝您的回覆.........所以我需要改變我xml文件也.....也可以使用同一個嗎? – Haris

+0

謝謝......我會試試這個代碼...... – Haris