SimpleCursorAdapter正在难以使用 [英] SimpleCursorAdapter is being difficult to use
问题描述
我一直停留在从长一个时间点,即与使用
I have been stuck at one point from long time, i.e with the use of
SimpleCursorAdapter
,因为它在返回正确的值失败。我见过很多类似的帖子在SO本身,说我应该在游标数据库查询添加_id列,而我应该做的。
as it fails while returning the correct value. I have seen similar many post in SO itself, saying that I should add _id column in the cursor database query, rather I should do
db.rawQuery(String,String)
在OnCreate(..)我的code是
My code in the onCreate(..) is
HospitalData = new Database(this);
HospitalData.open();
Cursor c = HospitalData.getAllRows_Patient_Db();
startManagingCursor(c);
c.moveToFirst();
//HERE SOME LOOP IS NEEDED FOR TRAVERSING AND PUTTING IN THE LISTVIEW
while(c.isAfterLast() == false)
{
String[] columns = new String[] { c.getString(1), c.getString(2) };
int[] to = new int[] { R.id.room_number_db, R.id.pt_initial_db };
adapter = new SimpleCursorAdapter(this,R.layout.patient_db, c,columns,to);
c.moveToNext();
}
setListAdapter(adapter);
和previously我的数据库访问code是如下:
And previously my database accessing code was as follows
public Cursor getAllRows_Patient_Db()
{
return db.query(DATABASE_PATIENT_TABLE, new String[] {KEY_ROWID, KEY_ROOM_NUMBER,
KEY_PATIENT_INITIAL
},
null,
null,
null,
null,
null);
}
,其中KEY_ROWID定义如下
where KEY_ROWID is defined as follows
public static final String KEY_ROWID = "_id";
而与此误差
07-04 22:10:23.301: ERROR/AndroidRuntime(16795): Caused by: java.lang.IllegalArgumentException: column '90' does not exist
07-04 22:10:23.301: ERROR/AndroidRuntime(16795): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
07-04 22:10:23.301: ERROR/AndroidRuntime(16795): at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:312)
下面列90不是列ID但据我的数据库是存储在cursor.getString数据(1),但我认为这是试图寻找cursor.getString(0),这是该行的ID。
Here column 90 is not the column id but according to my database is the data stored in cursor.getString(1), but I think here it is trying to search cursor.getString(0) which is the row id.
后来我改变了我的code如下:
Later I changed my code as follows
public Cursor getAllRows_Patient_Db()
{
String db_sel = "SELECT id as _id, KEY_ROOM_NUMBER" +
",KEY_PATIENT_INITIAL FROM DATABASE_PATIENT_TABLE";
return db.rawQuery(db_sel,null);
}
不过还是我收到错误,这个时间误差是不同的。
But still I am getting error, this time error is different
07-04 21:36:12.510: ERROR/global(9861): Deprecated Thread methods are not supported.
07-04 21:36:12.950: ERROR/AndroidRuntime(9861): FATAL EXCEPTION: main
07-04 21:36:12.950: ERROR/AndroidRuntime(9861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pro/com.pro.CopyOfFirstScreen}: android.database.sqlite.SQLiteException: no such table: DATABASE_PATIENT_TABLE: , while compiling: SELECT id as _id, KEY_ROOM_NUMBER,KEY_PATIENT_INITIAL FROM DATABASE_PATIENT_TABLE
07-04 21:36:12.950: ERROR/AndroidRuntime(9861): Caused by: android.database.sqlite.SQLiteException: no such table: DATABASE_PATIENT_TABLE: , while compiling: SELECT id as _id, KEY_ROOM_NUMBER,KEY_PATIENT_INITIAL FROM DATABASE_PATIENT_TABLE
我坚持了下来,从很长的时间,请大家帮忙!
I am stuck with it from very long time, please help!!
编辑:好的,现在有你们帮助我的查询语句是正确的,感谢您,我很抱歉,我不理解的数据库查询语法不错,但我想学习
EDIT : Okay now with you guys help my query statement is correct and thanks for that, I am sorry I am not good in understanding the syntax for database query but I am trying to learn
更改后的工作查询
String db_sel = "SELECT _id as _id, room_number" +",patient_initial FROM " + DATABASE_PATIENT_TABLE;
其实我不得不改变与键值声明字符串
Actually I had to change the declared String with the key values
public static final String KEY_ROWID = "_id";
public static final String KEY_ROOM_NUMBER = "room_number";
public static final String KEY_PATIENT_INITIAL = "patient_initial";
但现在我看到的另一个问题,是不是从查询语句,但我访问或使用simplecursorAdapter的方式
But now I see another problem that is not from the query statement but the way I am accessing or using the simplecursorAdapter
正如你可以看到我的表中有2行3列,现在我要来从列2和列3中的数据,并把它放在我的列表视图。
但我得到另一个错误从查询修复后
As you can see that my table has 2 rows and 3 columns, Now I want to fetch the data from column 2 and column 3 and put it in my listview. But after the fix from the query I am getting another error
这本来就是
String[] columns = new String[] {c.getString(1), c.getString(2) };
int[] to = new int[] { R.id.room_number_db, R.id.pt_initial_db };
adapter = new SimpleCursorAdapter(this,R.layout.patient_db, c,columns,to);
c.moveToNext();
setListAdapter(adapter);
和错误是
07-05 21:32:29.228: ERROR/AndroidRuntime(1505): Caused by: java.lang.IllegalArgumentException: column '90' does not exist
07-05 21:32:29.228: ERROR/AndroidRuntime(1505): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
07-05 21:32:29.228: ERROR/AndroidRuntime(1505): at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:312)
android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:312)
android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:312)
正如你可以看到它正在试图访问访问行的第一列inspite的数据
As you can see it is trying to access the data of first column inspite of accessing the row
之后,我改变了我的code更改
After that I made changes in my code
int x = 0;
String[] columns = new String[] { c.getString(0),c.getString(1), c.getString(2) };
int[] to = new int[] { x, R.id.room_number_db, R.id.pt_initial_db };
adapter = new SimpleCursorAdapter(this,R.layout.patient_db, c,columns,to);
c.moveToNext();
setListAdapter(adapter);
这是仅仅是为了一个猜测,找出SimpleCursorAdapter运行方式不同与普通的光标,我得到的错误是
This was only a guess in order to find out how differently SimpleCursorAdapter works compared to a normal cursor and the error I get is
07-05 21:34:47.947: ERROR/AndroidRuntime(1966): Caused by: java.lang.IllegalArgumentException: column '1' does not exist
07-05 21:34:47.947: ERROR/AndroidRuntime(1966): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
我知道这个问题正变得太长:-(做ü建议我从这里删除一些code。
I know this question is becoming too long :-( do u suggest me to remove some code from here.
推荐答案
在解决问题,更主要的是,我要确保我总是查询每个查询的数据库的_id,但如果我这样做cursor.getString(someIndex),那么这被作为的误导在我的情况适配器正在第一列的ROWID的内容,并试图寻找该行。
The problem is fixed, the main thing is that I should make sure I always query for the _id with each query to the database, but if I do it with cursor.getString(someIndex) then this gets misleaded as the adapter in my case is taking the content of first column as the rowid and is trying to search the row.
虚空做rawQuery(),或使用选择_id为_id的东西是强制性的。这些东西是因为我觉得有时候误导。
Nether doing rawQuery(),or using "Select _id as _id" stuffs are mandatory. these things are sometime misleading as I felt.
所以,让事情简单以下是code。
数据库code
So keeping things simple below is the code. Database code
public Cursor getAllRows_Patient_Db()
{
return db.query(DATABASE_PATIENT_TABLE, new String[] {
KEY_ROWID,
KEY_ROOM_NUMBER,
KEY_PATIENT_INITIAL
},
null,
null,
null,
null,
null);
}
活动code
HospitalData = new Database(this);
HospitalData.open();
Cursor c = HospitalData.getAllRows_Patient_Db();
startManagingCursor(c);
String[] columns = new String[] {HospitalData.KEY_ROOM_NUMBER,HospitalData.KEY_PATIENT_INITIAL };
int[] to = new int[] { R.id.room_number_db, R.id.pt_initial_db };
adapter = new SimpleCursorAdapter(this,R.layout.patient_db, c,columns,to);
c.moveToNext();
setListAdapter(adapter);
这篇关于SimpleCursorAdapter正在难以使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!