【问题标题】:An IF statement in a cursor adapter?游标适配器中的 IF 语句?
【发布时间】:2010-07-15 23:18:20
【问题描述】:
private void datafill()
{
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor);

        /* JournalRowId is the row id from the first database containing all journal names
        All notes are kept in database 2. I want only the notes that correspond to each 
        journal to be listed, KEY_HOMEID is the non visible field that shows where 
        each note came from.

         * 
         */
       if (editjournalDbAdapter.KEY_HOMEID == journalRowId){
        String[] from =  new String[]{editjournalDbAdapter.KEY_HEIGHT};

        int[] to = new int[]{R.id.detail1};
      }
            //Error here "from" and "to" are not defined outside of if statement
        SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.journaldetailrow, notesCursor, from, to);
        setListAdapter(notes);
    }

【问题讨论】:

  • 我在上面遇到了一个错误。即使我在 IF 之前定义了变量。是否可以使用上面的代码?...(如果记录不对应则跳过的if语句)

标签: java android sqlite cursor


【解决方案1】:

“from”和“to”只存在于 if() 语句的范围内。否则无论如何都没有多大意义 - 即使他们这样做了,它们的内容也将是未定义的(或者,如果是 Java,则为 null)并立即使您的应用程序崩溃。

我不知道您要完成什么,但您可能也希望 if() 块中的底部两个语句。

【讨论】:

  • 对不起,这是我多年来第一次使用 java。这是工作列表适配器的代码,没有 IF 语句。我想防止列出记录,除非特定字段与变量匹配。
猜你喜欢
  • 2013-02-28
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
相关资源
最近更新 更多