0

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. 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.

4

2 回答 2

0

使用List<BasicNameValuePair>它们将以键值方式存储。当您迭代存储它们时,将它们添加到列表中,例如:

List<BasicNameValuePair> latLongList = new ArrayList<BasicNameValuePair>();
 for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
        latLongList.add(new BasicNameValuePair(c.getString(latitude), c.getString(longitude)));
    }

然后你可以在任何你想要的地方使用它。在这种情况下,纬度将是关键,经度将是价值。

这是这样做的一种方式。相反BasicNameValuePair,您可以android.location.Location以相同的方式使用和设置。

于 2013-03-03T13:46:29.980 回答
0

我认为你会遇到比使用二维数组更值得的麻烦。如果您想保留 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++;
        }

就个人而言,我宁愿用一个对象来将这两者Strings结合在一起。这样,万一索引由于某种原因不匹配,您就可以放心了。

于 2013-03-03T14:03:38.750 回答