繁体   English   中英

Android Listview SimpleCursorAdapter错误

[英]Android Listview SimpleCursorAdapter Error

“ 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); }

我想做的是,根据“微调器”中选择的类别,从“ 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);  }

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

好的,现在可以解决先前的错误(如上所述)。 我必须在布局文件中修复微调器。 但是,尽管现在没有错误(运行时或编译时),但是代码在逻辑上并没有执行我打算执行的操作。 旋转器存在,但是在选择其他选项时,没有任何变化。

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

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

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

校验

Java中==和equals()之间有什么区别?

你有

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM