【问题标题】:Android Listview SimpleCursorAdapter ErrorAndroid Listview SimpleCursorAdapter 错误
【发布时间】:2014-04-12 01:12:33
【问题描述】:

“QuoteTable”是一个定义数据库的java类文件。下面的代码运行良好。

// mCategory = (Spinner) findViewById(R.id.category); - created during OnCreate method 

private void fillData() {

// Fields from the database (projection)
// Must include the _id column for the adapter to work
String category = (String) mCategory.getSelectedItem();
String[] from = new String[] { QuoteTable.COLUMN_QUOTE };
// Fields on the UI to which we map
int[] to = new int[] { R.id.label };

getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.motivate_row, null, from,
    to, 0);

setListAdapter(adapter); }

我想要做的是,根据 Spinner 中选择的类别从“QuoteTable”中选择特定的引号。此类别应与“QuoteTable”中的 COLUMN_CATEGORY 字段匹配。所以我尝试了这个 -

 private void fillData() {
String[] from = null;
// Fields from the database (projection)
// Must include the _id column for the adapter to work
if(category.equals("All")){
       from = new String[] { QuoteTable.COLUMN_QUOTE };
}
else{
        if(category.equals(QuoteTable.COLUMN_CATEGORY))
              from = new String[] { QuoteTable.COLUMN_QUOTE };
}
// Fields on the UI to which we map
int[] to = new int[] { R.id.label };

getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.motivate_row, null, from,
    to, 0);

setListAdapter(adapter);  }

/* 这个错误已解决 */ 这给了我一个“SimpleCursorAdapter”错误if(category == "All") 为什么会这样?我已经在另一个类中使用 Cursor 来运行查询以保存数据库。这只是我的“视图”,我不想重复使用该代码。

好的,现在前面的(上面提到的)错误已经解决了。我必须在布局文件中修复微调器。然而,虽然现在没有错误(运行时或编译时),但代码在逻辑上并没有按照我的意图去做。微调器存在,但在选择不同的选项时,没有任何变化。

【问题讨论】:

    标签: android sqlite listview cursor simplecursoradapter


    【解决方案1】:

    字符串比较应该使用.equals()方法:"All".equals(category)

    【讨论】:

    • 好吧,我已经更正了。现在我得到一个空指针异常
    【解决方案2】:

    你应该使用.equals()来比较字符串

    if(category.equals("All")){
    

    检查

    What is the difference between == vs equals() in Java?

    你有

    adapter = new SimpleCursorAdapter(this, R.layout.motivate_row, null, from,
    to, 0);
    

    第三个参数为null应该是一个游标

    【讨论】:

    • 好吧,我已经更正了。现在我得到一个空指针异常
    • @user2201545 发布堆栈跟踪
    • @user2201545 游标适配器构造函数的第三个参数为空
    • @user2201545 通过提取异常部分发布堆栈跟踪或忘记进一步的帮助。链接无济于事
    • 致命异常:主要 java.lang.RuntimeException:无法启动活动 ComponentInfo{b.example.m/b.example.m.mOverviewActivity}:java.lang.NullPointerException
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多