android listview 切换,Android ListView切换按钮

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

如何在列表视图中添加切换按钮?

R.layout.alarm_list:

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/no_reminders"

android:textColor="#FFF"/>

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:

android:id="@+id/text1"

android:padding="10dip" android:layout_width="242dp"

android:layout_height="wrap_content"/>

我的项目被推迟了.

救命

解决方法:

没有任何摘要.解决你的问题.我认为您需要进行多重选择.现在这是您需要的东西.

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

> newView您将在其中通过扩大R.layout.alarm_row(它也应该包含切换按钮)来创建列表项视图的地方.您使切换按钮不可单击.

> bindView,您将在其中设置切换按钮和文本视图的文本状态

这就是您在“活动”方面需要的内容.

>您已通过xml中的android:choiceMode或使用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);

参考文档:

标签:android,android-listview,listadapter,simplecursoradapter

来源: https://codeday.me/bug/20191009/1877677.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值