[英]How to fetch multiple items from android sqlite database and store it in array?
I m using static database to fetch data. 我正在使用静态数据库来获取数据。 I m fetching longitude and latitude of static location from database, storing it in string and using.
我从数据库中获取静态位置的经度和纬度,将其存储在字符串中并使用。 What if i want to extract all longitude and latitude and store it in an array...i mean array of size [137][15] storing latitude and longitude in a single 2d array.....or 2 separate 1 d array.
如果我想提取所有经度和纬度并将其存储在数组中,该怎么办...我的意思是大小[137] [15]的数组,将纬度和经度存储在单个2d数组中.....或2个单独的1d数组。 One for latitude ..another for latitude...
一个用于纬度..另一个用于纬度...
DataBaseHelper2 mDbHelper = new DataBaseHelper2(this);
try {
mDbHelper.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mDbHelper.openDataBase();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Spinner s1=(Spinner) findViewById(R.id.spinner1);
int index1=s1.getSelectedItemPosition();
String Text1 = s1.getSelectedItem().toString();
String Text3="";
String Text4="";
if(index1==0)
{
Toast.makeText(getApplicationContext(),"Please Enter station", Toast.LENGTH_SHORT).show();
}
else{
Cursor c=mDbHelper.fetchQuery(Text1);
int latitude = c.getColumnIndex("lat");
int longitude = c.getColumnIndex("lng");
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
Text3=Text3 + c.getString(latitude)+"\n";
Text4=Text4 + c.getString(longitude)+"\n";
}
Double p,p1;
p=Double.valueOf(Text3).doubleValue();
p1=Double.valueOf(Text4).doubleValue();
fetchquery is a method to extract longitude and latitude where name of station matches with text of spinner1. fetchquery是一种提取经度和纬度的方法,其中测站的名称与spinner1的文本匹配。
Use List<BasicNameValuePair>
they will be stored in key-value manner. 使用
List<BasicNameValuePair>
它们将以键值方式存储。 As you iterate store them add them in the list like: 迭代存储它们时,将它们添加到列表中,如下所示:
List<BasicNameValuePair> latLongList = new ArrayList<BasicNameValuePair>();
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
latLongList.add(new BasicNameValuePair(c.getString(latitude), c.getString(longitude)));
}
Then you can use it where ever you want. 然后,您可以在任何需要的地方使用它。 In thiscase latitude will be key and longitude will be value.
在这种情况下,纬度将是关键,经度将是值。
This is one way of doing this. 这是这样做的一种方式。 Also instead
BasicNameValuePair
you can use android.location.Location
and set in same manner. 同样,可以使用
android.location.Location
代替BasicNameValuePair
并以相同的方式进行设置。
I think you'll run into more trouble than it's worth using a 2 dimensional array for this. 我认为您会遇到更多麻烦,而不是为此使用二维数组。 If you wanna persist 2 regular arrays, then perhaps you could check out the following code:
如果要保留2个常规数组,则可以检查以下代码:
if(index1==0){
Toast.makeText(getApplicationContext(),"Please Enter station", Toast.LENGTH_SHORT).show();
}else{
Cursor c=mDbHelper.fetchQuery(Text1);
arrLatitude = new String [c.getCount()];
arrLongitude = new String [c.getCount()];
int latitude = c.getColumnIndex("lat");
int longitude = c.getColumnIndex("lng");
i = 0;
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
Text3=Text3 + c.getString(latitude)+"\n";
Text4=Text4 + c.getString(longitude)+"\n";
arrLatitude[i] = Text3;
arrLongitude[i] = Text4;
i++;
}
Personally, I'd rather do it with an object to hold both of these Strings
together. 就个人而言,我宁愿使用一个对象来将这两个
Strings
结合在一起。 That way you'd have peace of mind in case the indexes become mismatched for whatever reason. 这样一来,即使索引由于任何原因不匹配,您都可以放心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.