【问题标题】:ListView not working after i changed its table. AndroidListView 在我更改其表后无法正常工作。安卓
【发布时间】:2012-06-24 08:02:56
【问题描述】:

我创建了一个列表视图来在屏幕上显示数据库的所有内容。我使用的代码是这样的

 private void fillData() {
// Get all of the notes from the database and create the item list
   Cursor c = datasource.getAllActs();
    startManagingCursor(c);
    String[] from = new String[] {DataBaseHelper.KEY_DATE,  
    DataBaseHelper.KEY_STEPS,DataBaseHelper.KEY_CALs };
    int[] to = { R.id.code, R.id.Days, R.id.BMI };
    SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.notes_row, c,  from, to);
   setListAdapter(notes);               
 }

它在下面的代码中工作。

 private void fillData() {
            // Get all of the notes from the database and create the item list
            Cursor c = datasource.getAllGoals();
            startManagingCursor(c);
            String[] from = new String[] {DataBaseHelper.KEY_GOAL, DataBaseHelper.KEY_Current,DataBaseHelper.KEY_Target };
            int[] to = { R.id.code, R.id.Days, R.id.BMI };
            SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.notes_row, c, from, to);
            setListAdapter(notes);              
     }

我只是想知道我可能遗漏的这段代码是否有问题。如果你需要的话,我有 logcat

【问题讨论】:

  • 忘记logcat...从你的帖子我什至无法判断问题是什么。
  • 请发布 logcat 错误,如果 Alex 没有猜到,我们可以看看问题出在哪里。

标签: android sqlite android-listview


【解决方案1】:

我唯一能想到的是您的新Cursor 没有名为_id 的列。确保光标,

Cursor c = datasource.getAllActs();

有一个名为 _id 的列,因为 SimpleCursorAdapter 类需要它。

【讨论】:

  • 可能是这种情况,因为 _ID 不是 gatAllActs(); 表的一部分。正在访问。
  • Alex 也确实有几个具有相同 _id 字段的表会导致任何问题
  • 不。事实上,让所有表都包含一个自动递增的主键(即_id)通常被认为是一种很好的做法。每个表的列完全独立于任何其他表的列。除非你在做某种复杂的JOIN,否则应该没有问题。
  • 我有点想,在创建主键时,字段名称需要是唯一的。所以我可以有 3 个表,每个表都有相同的主键,名称为“_id”
  • 假设您有两个表table1table2,它们都有一个名为_id 的列。然后在 SQLite 语法中,您将使用 table1._idtable2._id 引用这些列,因此您会看到它们都是唯一的。唯一的限制是一个表不能包含两个同名的列(显然)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
相关资源
最近更新 更多