【发布时间】: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