2014-06-17 178 views
1

我已經完成了使用SQLite,但無法存儲這些值。我通過ListView檢索了這個值。任何人都可以幫忙嗎?我對這個項目感到震驚。如何在數據庫中存儲經緯度詳細信息並在ListView中查看詳細信息

package com.mahesh.map2; 
public class history extends ListActivity { 

    //private static final String DATABASE_TABLE = null; 

    private ArrayList<String> results = new ArrayList<String>(); 
    // private static final String DATABASE_TABLE = "locations"; 

    private static String DBNAME = "locationmarkersqlite"; 

    /** Field 1 of the table locations, which is the primary key */ 
    public static final String FIELD_ROW_ID = "_id"; 

    /** Field 2 of the table locations, stores the latitude */ 
    public static final String FIELD_LAT = "lat"; 

    /** Field 3 of the table locations, stores the longitude*/ 
    public static final String FIELD_LNG = "lng"; 

    /** Field 4 of the table locations, stores the zoom level of map*/ 
    public static final String FIELD_ZOOM = "zom"; 

    /** A constant, stores the the table name */ 
    private static final String DATABASE_TABLE = "locations"; 

    /** An instance variable for SQLiteDatabase */ 
    private SQLiteDatabase mDB; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     openAndQueryDatabase(); 

     displayResultList(); 


    } 
    private void displayResultList() { 
     TextView tView = new TextView(this); 
     tView.setText("This data is retrieved from the database and only 4 " + 
       "of the results are displayed"); 
     getListView().addHeaderView(tView); 

     setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, results)); 
     getListView().setTextFilterEnabled(true); 

    } 
    private void openAndQueryDatabase() { 
     try { 
      LocationsDB dbHelper = new LocationsDB(this.getApplicationContext()); 
      mDB = dbHelper.getWritableDatabase(); 
      //double latitude; 
      //double longitude; 
      Cursor c = mDB.query("locations", new String[]{ 
        "latitude", "longitude"}, null, null, 
        null, null, null); 
      if (c != null) { 
       if (c.moveToFirst()) { 
        do { 
        // String title = c.String(c 
        //   .getColumnIndex("title")); 
        // String description = c.String(c 
         //   .getColumnIndex("description")); 
         int latitude = (int) (c.getDouble(c 
           .getColumnIndex("latitude")) * 1E6); 
         int longitude = (int) (c.getDouble(c 
           .getColumnIndex("longitude")) * 1E6); 
         results.add("FIELD_LNG" + latitude + ",FIELD_LAT " + longitude); 
        }while (c.moveToNext()); 
       } 
      }   
     } catch (SQLiteException e) { 
      Log.e(getClass().getSimpleName(), "Could not create or Open the database"); 
     } finally { 
      if (mDB != null) 
       mDB.execSQL("DELETE FROM " + DATABASE_TABLE); 
       mDB.close(); 
     } 

    } 

} 
+0

你在哪裏創建數據庫? – zozelfelfo

回答

0

我使用ORMLite庫來管理Db的東西。這很容易實現,不應超過4小時,但它將來會非常有價值。首先,你必須創建一些你想要在db中存儲的對象並添加註釋,以便ORMLite知道什麼和如何存儲。

public class MyTableRow { 
    @DatabaseField 
    long longitude; 
    @DatabaseField 
    long latitude; 
    @DatabaseField(id=true) 
    int id; 
    public MyTableRow(long lon,long lat){..}; 
} 

之後,建立連接源:

MySqliteOpenHelper dbHelper = null; 
       AndroidConnectionSource connectionSource = null; 
       if (dbHelper == null || connectionSource == null) { 
        dbHelper = new MySqliteOpenHelper(context, null, null, 
          0); 
        connectionSource = new AndroidConnectionSource(dbHelper); 
       } 

MySqliteOpenHelper可以是一個簡單的配置類,與數據庫名稱和版本:

public class MySqliteOpenHelper extends OrmLiteSqliteOpenHelper { 

private static final String DATABASE_NAME = "test"; 
private static final int DATABASE_VERSION = 2; 

public MySqliteOpenHelper(Context context, String databaseName, 
     CursorFactory factory, int databaseVersion) { 
    super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { 

} 

@Override 
public void onUpgrade(SQLiteDatabase database, 
     ConnectionSource connectionSource, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 

} 
} 

一旦這些設置,你就可以開始創建表格並插入和檢索數據:

public static void createTables(ConnectionSource connectionSource, 
     Class<?>... classes) { 
    for (Class<?> c : classes) { 
     try { 
      TableUtils.createTableIfNotExists(connectionSource, c); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
private static <T> void doInserts(ConnectionSource connectionSource, 
     Class<T> object, List<T> objects) 
     throws SQLException { 
      final Dao<T, String> dao = DaoManager.createDao(connectionSource, 
      object); 

    for (T data : objects) { 
     dao.createOrUpdate(data); 
    } 

} 
public static <T> List<T> getTable(Class<T> objectClass, 
     Context context) { 
    MySqliteOpenHelper sqliteDbHelper = new MySqliteOpenHelper(context, 
      null, null, 0); 
    ConnectionSource connectionSource = new AndroidConnectionSource(
      sqliteDbHelper); 

    List<T> mList; 
    try { 
     Dao<T, String> mDao = DaoManager.createDao(connectionSource, 
       objectClass); 
      mList = mDao.queryForAll(); 
     return mList; 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
+0

可以告訴我如何存儲緯度和緯度的詳細信息到我試過的sqlite數據庫,但它不是開放 – user3747154

+0

從[這裏]下載核心和Android ORMLite 將兩個jar放在Eclipse libs文件夾中。之後,接受我提供的代碼,自行調整。然後,將lat/long放入包裝對象中,並將該對象添加到列表中:myObjectList.add(new MyTableRow(lat,long);'並插入數據庫doInserts(source,MyTableRow.class,myObjectList);' 。檢索數據'dataFromDb = getTable(MyTableRow.class,someContext);'。 – Marius