【问题标题】:ListActivity with Cursor带光标的 ListActivity
【发布时间】:2010-07-20 05:03:44
【问题描述】:

有人知道使用 CursorAdapter 的简单示例吗?这就是我现在正在做的事情,它因 RuntimeException 而崩溃。鉴于我是新手并且找不到任何使用游标的 ListView 的简单示例,因此我确定它很简单。

谢谢,


...

公共最终类 MyListActivity 扩展 ListActivity { 私有类 MyCursorAdapter 扩展 CursorAdapter { public MyCursorAdapter(上下文上下文,光标光标) { 超级(上下文,光标); // 碰撞 ...

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myDB_ = new MyDB(this);
    myDB_.open();

    Cursor cursor = myDB_.read();
    startManagingCursor(cursor);

    MyCursorAdapter adapter = new MyCursorAdapter(this, cursor);

...

【问题讨论】:

    标签: android listview cursor adapter


    【解决方案1】:

    Android 开发者资源中的记事本教程使用带有 ListView 的 CursorAdapter。 您可以在此处找到本教程的相关部分: http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html

    【讨论】:

    • 我没有看到CursorAdapter,我看到的是SimpleCursorAdapter,我怀疑它很相似,但不一样。如果您查看我的示例代码,我正在使用 CursorAdapter。我正在尝试根据数据库中的数据显示图像。在我的情况下,如果存储的值是正数或负数,它只是一个不同的图形。如果 SimpleCursorAdapter 可以解决这个问题,请告诉我。否则,我假设它不会那样做。
    • SimpleCursorAdapter 的文档说它可以映射到 ImageViews。 developer.android.com/reference/android/widget/…
    • 我没有使用 ImageView,我使用的是 ImageButton。此外,我不确定他们如何知道如何映射我的价值观。它如何知道正值或负值是否会映射到我的 ImageButton 上的不同图像。因此,我认为我需要使用 CursorAdapter 并自己编写映射的原因。是吗?
    • 据我所知,是的。很抱歉没有提供太多帮助:/
    【解决方案2】:

    您可以在 SimpleCursorAdapter 上使用 setViewBinder 将值映射到 SimpleCursorAdapter 本身不支持的视图。您可以在此处查看使用 setViewBinder 将数据从内容提供程序绑定到 CheckBox 的示例:CheckBox checked state in a ListView

    您可以使用setViewBinder 将您的图像绑定到您的imageButtons。这样,您就不必创建自己的 ListAdapter。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多