我觉得我错过了一些简单而愚蠢的东西。我有一个列表视图,顶部有几个按钮。列表视图最初填充了数据。当您单击按钮时,列表视图应该根据 Where 语句中更改的变量填充其自身。实际上,我可能只是开始一个新的列表活动,但我觉得有更好的方法。
我一直在阅读,CursorAdapter.changeAdapter()
但notifydatasetchanged()
我还没有实现这个,因为我有一个更基本的问题。
我可以成功查询数据库并在列表中显示静态结果。当我尝试将流程分解为多个步骤时,我遇到了 ERROR: Invalid statement in fillWindow
. 据我所知,这是由于不正确地关闭游标数据库和数据库助手造成的,因此人们使用内容提供程序。
现在我只是想让这个工作。
public class DListView extends ListActivity implements OnClickListener{
public static final String NAME = "Name";
public static final String DESCRIPT = "Description";
public static final String DATABASE_TABLE = "Table";
public static final String DAY = "Day_id";
/** Called when the activity is first created. */
private Cursor c = null;
private String[] colsfrom = {"_id", NAME, DESCRIPT, DAY};
private int[] to = new int[] {R.id.text01, R.id.text02, R.id.text03, R.id.text04};
public int b = 0;
public int d = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinklistview);
View left = findViewById(R.id.left_button);
left.setOnClickListener(this);
View right = findViewById(R.id.right_button);
right.setOnClickListener(this);
Intent thisIntent = getIntent();
b = thisIntent.getIntExtra("_b", 0);
//0 is the default argument is nothing is passed.
d = thisIntent.getIntExtra("_d", 0); //same idea as above.
c = fillList();
/*this creates a new cursor adapter
@param Context is the list context that you will be filling.
@param int layout is the layout that you will use for the rows
@param Cursor is the cursor that was returned from the query
@param from is the column names
@param to is the layout ids that the fields will be put in.
@param from is the column names to map from
@param to is the layout ids that the column fields will be put in.
*/
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.row, c, colsfrom, to);
setListAdapter(myAdapter);
}
private Cursor fillList() {
DBHelper DbHelper = new DBHelper(this);
Cursor cursor;
String wHERE = "_id = " + b + " AND Day_id = " + d ;
try {
myDbHelper.openDataBase();
}
catch(SQLException sqle){
throw sqle;
}
cursor = myDbHelper.getDrinks(DATABASE_TABLE, colsfrom, wHERE, null, null,null, null);
myDbHelper.close();
return cursor;
}
当我将 fillList() 的内容放入 onCreate() 时,它显示数据就好了。当我把它拔出来时,它给了我错误。为什么会这样?如果有人有更好的解决方法,我很乐意阅读它。或者我们可以玩一个游戏,叫做“我现在做错了什么愚蠢的事情?谢谢。
编辑:来自 DBHelper
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
我在想我的问题线是 super.close() 我相信这条线会关闭数据库以及与之相关的任何东西,这意味着我在关闭后尝试使用的光标。不过我可能是错的。如果可以请解释一下。