【问题标题】:Android listview toggle buttonAndroid 列表视图切换按钮
【发布时间】:2011-12-06 10:00:31
【问题描述】:

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

如何在ListView 中添加Toggle 按钮?

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

我的项目被推迟了。

帮助

【问题讨论】:

标签: android android-listview listadapter simplecursoradapter


【解决方案1】:

没有小的sn-p ans。你的问题。我假设您需要进行多项选择。现在这里有你需要的东西。

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

  • newView 您将通过膨胀R.layout.alarm_row 来创建列表项视图(它也应该包含您的切换按钮)。您已将切换按钮设为不可点击。
  • bindView 您将为文本视图设置切换按钮和文本的状态

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

  • 您已通过android:choiceMode in xml 或使用setChoiceMode 将ListView 设置为多选模式。

现在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 docs

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

【讨论】:

    【解决方案2】:

    尝试创建一个自定义适配器,例如:

    public class YourAdapter extends BaseAdapter
    

    以及带有切换按钮的行的自定义布局(您必须在方法 geView 中扩展布局)。

    你可以在这里找到一个例子:http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

    希望这会有所帮助..

    【讨论】:

    • 我已经更改了第二个布局文件(R.layout.alarm_row),如下所示。这样做之后,我无法为创建的行设置 onclick 监听器。
    • schemas.android.com/apk/res/android"> schemas.android.com/apk/res/android" android:id="@+id/text1" android:padding="10dip" android:layout_width="242dp" android: layout_height="wrap_content"/>
    猜你喜欢
    • 2012-10-16
    • 2017-07-24
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多