简体   繁体   English

在Cursor中的simple_list_item_checked中设置复选框

[英]Set checkbox in simple_list_item_checked from Cursor

I am beginner in Android developing. 我是Android开发的初学者。

I have a ListView of type android.R.layout.simple_list_item_checked and I am trying to set the checkbox to value, which is in my Cursor under name "CHECKED". 我有一个类型为android.R.layout.simple_list_item_checked的ListView,并且我试图将复选框设置为value,这在我的Cursor中的名称为“ CHECKED”。 The problem is that I cannot find the id of the checkbox (I have tried android.R.id.checkbox, but it hasn't worked) and I don't know, how to set it based on my Cursor. 问题是我找不到复选框的ID(我尝试过android.R.id.checkbox,但没有用),我也不知道如何根据我的Cursor设置它。 The cursor is created from SQLite database table, where the "CHECKED" column is BOOLEAN type. 游标是从SQLite数据库表创建的,其中“ CHECKED”列为BOOLEAN类型。

My code, which is not working: 我的代码不起作用:

lv.setAdapter(
                new SimpleCursorAdapter(MainActivity.this, android.R.layout.simple_list_item_checked, mCursor,
                        new String[]{"NOTES", "CHECKED"}, new int[]{android.R.id.text1, android.R.id.checkbox}, 0));

Could you help me, please? 请问你能帮帮我吗? Thanks in advance. 提前致谢。

如果您查看R.layout.simple_list_item_checked xml文件,您将看到复选框ID为@android:id/text1

The short answer is that SimpleCursorAdapter will not suffice for the task you want. 简短的答案是SimpleCursorAdapter 无法满足您想要的任务。

If you look inside into its public void bindView(View view, Context context, Cursor cursor) method (this is where the magic happens and your views are attached to the data you want to see) you can verify how the SimpleCursorAdapter does this, and in its logic there is no use case for binding a CheckedTextView 如果您查看其public void bindView(View view, Context context, Cursor cursor)方法(这是发生魔术的地方,并且您的视图已附加到要查看的数据上),则可以验证SimpleCursorAdapter如何做到这一点,以及在其逻辑上没有绑定CheckedTextView用例

You can see for yourself here in Google's code : 您可以在此处通过Google的代码亲自查看:

if (v instanceof TextView) {
   setViewText((TextView) v, text);
} 
else if (v instanceof ImageView) {
          setViewImage((ImageView) v, text);
     } 
else {
       throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleCursorAdapter");
                    }

The only reason you are not getting the IllegalStateException is because CheckedTextView extends TextView. 没有得到IllegalStateException的唯一原因是CheckedTextView扩展了TextView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM