Android 列表视图切换按钮 [英] Android listview toggle button

查看:46
本文介绍了Android 列表视图切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Listview 将列出数据库中的警报.我需要在每个列表项旁边添加一个 Toggle 按钮来设置警报开/关.

I have a Listview that will list the alarms which are in the database.I need to add a Toggle Button beside each list item to set the alarm on/off.

如何在 ListView 中添加 Toggle 按钮?

How can I add the Toggle Button in the ListView?

R.layout.alarm_list:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ListView android:id="@+id/android:list"
      android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
   <TextView android:id="@+id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_reminders"
        android:textColor="#FFF"/>
     </LinearLayout>

Java 代码:

      private void fillData() {
    Cursor remindersCursor = aDbHelper.fetchAllAlarms();
    startManagingCursor(remindersCursor);

    // Create an array to specify the fields we want to display in the list
    // (only TITLE)
    String[] from = new String[] { AlarmDbAdapter.KEY_TITLE };

    // and an array of the fields we want to bind those fields to (in this
    // case just text1)
    int[] to = new int[] { R.id.text1};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter reminders = new SimpleCursorAdapter(this,
            R.layout.alarm_row, remindersCursor, from, to);

    setListAdapter(reminders);
   }

R.layout.alarm_row:

   <?xml version="1.0" encoding="utf-8"?>

   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
    android:padding="10dip" android:layout_width="242dp"  
         android:layout_height="wrap_content"/>

我的项目被推迟了.

帮助

推荐答案

没有小片段 ans.你的问题.我假设您需要进行多项选择.现在这里有你需要的东西.

There is no small snippet ans. to your problem. I assume you need to have multi-selection. Now here are the things you need.

由于您使用的是 SimpleCursorAdapter,您应该将其替换为 CursorAdapter.为此,您必须扩展它,因为它是一个抽象适配器.完成后,您将覆盖两个函数.

Since you are using SimpleCursorAdapter, you should replace that with CursorAdapter. To do so you have to extend it as it is a abstract adapter. Once you done that you will be overriding two functions.

  • newView 您将在其中通过膨胀 R.layout.alarm_row(它也应该包含您的切换按钮)来创建列表项视图.您已将切换按钮设为不可点击.
  • bindView 您将在其中为文本视图设置切换按钮和文本的状态
  • newView Where you will create your list item views by inflating R.layout.alarm_row (it should contain your toggle button too). You have make toggle button non-clickable.
  • bindView where you will set state of toggle button and text for your text view

这就是您在活动方面所需要的.

That said this what you need on the Activity side.

  • 您已通过 xml 中的 android:choiceMode 或使用 setChoiceMode 将 ListView 设置为多选模式.
  • You have make your ListView to multi-selection mode by android:choiceMode in xml or using setChoiceMode.

现在 bindView 看起来像:

ListView lv = ((ListActivity)context).getListView();
// Containing all check states
SparseBooleanArray sba = lv.getCheckedItemPositions();


// I am using check box

cb.setChecked(false);

// Cursor is passed as an argument.
if(sba != null)
  if(sba.get(cursor.getPosition()))
     cb.setChecked(true);

参考文档:

CursorAdapter 文档

http://developer.android.com/reference/android/widget/ListView.html

这篇关于Android 列表视图切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆