【问题标题】:How to update SQLite database from listview using adapter in Android?如何在 Android 中使用适配器从 listview 更新 SQLite 数据库?
【发布时间】:2011-12-14 17:28:45
【问题描述】:

我的应用程序中有一个设置菜单。当我单击菜单按钮时,我可以单击设置图标并出现设置活动。设置布局包含一个列表视图,每个列表视图项由textview(设置描述)和edittext(设置数据)组成。我将数据存储在 SQLite 数据库的设置表中,并将所有设置添加到适配器 (ListAdapter) 和 listView,如下所示:

Cursor cursor = dbAdapter.fetchSettings();
startManagingCursor(cursor);
String[] from = new String[] { "description", "data" };
int[] to = new int[] { R.id.settingsDesc, R.id.settingsText };
listAdapter = new SimpleCursorAdapter(this,
R.layout.settings_menu_item, cursor, from, to);
listView.setAdapter(listAdapter);

如您所见,我可以更改设置数据。我的问题是,当我更改了任何 edittext 元素中的文本时,如何更新数据库?我在设置布局中也有保存按钮,我希望单击它会更新数据库。我希望有一个简单的方法来做到这一点。

【问题讨论】:

    标签: android android-adapter


    【解决方案1】:

    您可以获取所选 editText 值的 id 并触发更新查询以更新您的数据库。然后您可以使用cursor.requery() 重新填充 ListView 和 notifyDataSetChanged().

    所以,在保存按钮上

    1.) Fire the update query for the selected row.
    2.) cursor.requery();
    3.) adapter.notifyDataSetChanged();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      相关资源
      最近更新 更多