简体   繁体   English

如何从android sqlite数据库中获取多个项目并将其存储在数组中?

[英]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.

相关问题 如何从中心数据库获取数据并存储在android sqlite数据库中? - How to fetch data from center database and store in android sqlite database? 如何从mysql中获取数据并存储在android中的Sqlite数据库中 - How to fetch data from mysql and store in Sqlite database in android 从sqlite获取并存储列值到android(java)数组中 - Fetch and store a column values from sqlite in android(java) array Android:SQLite,如何从多个字段中获取? - Android: SQLite, How to fetch from multiple field? 如何在sqlite android中存储多个日期并获取它们以显示在列表中? - How to store multiple DATES in sqlite android and fetch them to display in a List? 如何从 SQLite 数据库中存储和获取大文本数据? - How to store and fetch large text data from SQLite database? 如何在Android中从sqlite存储和获取列表视图数据? - How to store and fetch list view data from sqlite in android? 如何创建数组并在android的sqlite数据库中获取数据 - how to create an array and fetch the data in sqlite database in android 如何使用baseAdapter从android中的sqlite数据库中删除多个Checked Listview项目 - How to delete multiple Checked Listview items from sqlite database in android using baseAdapter 如何将单行数据存储到android中的sqlite数据库的字符串数组中 - How to store a single row data into a string array of sqlite database in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM