java 如何从android中的游标中检索数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6395887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve data from a cursor in android?
提问by molleman
I retrieve information from my database and it is all in a Cursor. Now i need to go through each item in the cursor to retrieve lng lats and a name to display on a map.
我从我的数据库中检索信息,所有信息都在 Cursor 中。现在我需要遍历光标中的每个项目以检索 lng lats 和要显示在地图上的名称。
Cursor c = mDbHelper.fetchSpot(15);
startManagingCursor(c);
double lat = Double.parseDouble(c.getString(c.getColumnIndexOrThrow(ParkingSpotDBAdapter.KEY_LAT)));
double lng = Double.parseDouble(c.getString(c.getColumnIndexOrThrow(ParkingSpotDBAdapter.KEY_LNG)));
String name = c.getString(c.getColumnIndexOrThrow(ParkingSpotDBAdapter.KEY_ADDRESS));
I Have antoher method called fecthAllSpots()
this returns many items in the cursor, how do i go about retrieving each lng lat and name from each row in that cursor?
我有另一个调用fecthAllSpots()
此方法的方法返回游标中的许多项目,我如何从该游标中的每一行检索每个 lng lat 和名称?
回答by Flo
You can use a while loop to iterate of all row the cursor returns.
您可以使用 while 循环来迭代游标返回的所有行。
while(c.moveToNext()){
double lat = Double.parseDouble(c.getString(c.getColumnIndexOrThrow(ParkingSpotDBAdapter.KEY_LAT)));
double lng = Double.parseDouble(c.getString(c.getColumnIndexOrThrow(ParkingSpotDBAdapter.KEY_LNG)));
String name = c.getString(c.getColumnIndexOrThrow(ParkingSpotDBAdapter.KEY_ADDRESS));
}
Of course you have to do something with the lat, lng and name variable as they will get overridden on each iteration. I suggest you implement a DBLocation class which can store the name, longitude and latitude of a location. You can then store all the objects you create from the cursor in a array or list.
当然,您必须对 lat、lng 和 name 变量进行处理,因为它们会在每次迭代时被覆盖。我建议您实现一个 DBLocation 类,它可以存储位置的名称、经度和纬度。然后,您可以将通过游标创建的所有对象存储在数组或列表中。
回答by DArkO
First. you can just use the getInt, getLong etc.. methods on teh cursor to retrieve the values instead of parsing them afterwards.
第一的。您可以只使用游标上的 getInt、getLong 等方法来检索值,而不是事后解析它们。
The question:
问题:
here is the sintax:
这是sintax:
first query the db to get a cursor.
首先查询数据库以获取游标。
next do a null check on the cursor (just in case because it sometimes happens), doable with try catch block also.
接下来对游标进行空检查(以防万一,因为它有时会发生),也可以使用 try catch 块。
next write:
接下来写:
if(c.moveToFirst()){
do{
// YOUR CODE FOR EACH CURSOR ITEM HERE.
// the cursor moves one field at a time trhough it's whole dataset
}while(c.moveToNext())
}
at the end close the cursor or if it is a managed query do nothing the activity will close it for you. REmember all this in a block and apart for everything else you will be checking consider a nullPointerException for the cursor.
最后关闭游标,或者如果它是托管查询,则什么都不做,活动将为您关闭它。记住所有这些都在一个块中,除了您将检查的所有其他内容,请考虑游标的 nullPointerException。