列表视图,带有切换按钮

时间:2012-09-01 12:41:25

标签: android

我正在使用切换按钮编写类似列表视图的应用程序。 这是我的源代码。

   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>

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

当我点击切换按钮时,它会显示On和off。 但是当我点击列表时,我没有收到任何回复。

对于我的应用程序,我需要在按下切换按钮的同时发送一些数据。

1 个答案:

答案 0 :(得分: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>