2014-01-10 79 views
0

我試圖做一個單位轉換應用程序,截至目前我已經在string.xml文件這樣內進行了單元區域的陣列:如何創建SQLiteDatabase並使用它來填充微調器?

<string-array name="weight_name_array"> 
     <item>Gram</item> 
     <item>Pound</item> 
     <item>Ounce</item> 
    </string-array> 
    <string-array name="weight_value_array"> 
     <item>1.0</item> 
     <item>453.59237</item> 
     <item>28.3495</item> 
    </string-array> 

我想用替換此SQLiteDatabase包含兩個表,WeightLength,但我對使用SQLiteDB非常新,我試圖尋找指導,但似乎無法理解如何繼續。

我想要做的是在每個表中有兩列,unitNameunitValue,然後我想單位名稱顯示在微調框中。任何幫助表示讚賞。

回答

1

首先,您最好將它保存在XML值中。 沒有數字被改變.. 數據庫將不斷更改或添加數據,而不是永遠不會改變的靜態數據。

除此之外,還有大量關於如何在Android中使用SQLite數據庫的適當教程,因爲它是基本和核心功能。

嘗試Vogella.com只是谷歌,你不在那裏找到。

+0

的事情是,我希望用戶能夠添加新的測量領域和新的單位,所以這就是爲什麼我想在DB的。這可以通過其他方式完成嗎? – guitarzero

+0

不,那麼數據庫是一個好主意:) –

0

在Youtube上關注此問題以獲得基本的更好理解:http://www.youtube.com/watch?v=OUoGnvz_Yw4以及Youtube上的其他可用教程。隨着它,你可以注意 - http://www.vogella.com/tutorials/AndroidSQLite/article.html以及。

我在我的一個需求中使用了SQLiteDatabase作爲教程的一部分。它創建了表格,插入以及升級方法供您參考。我相信它會幫助你

StatusData.Java

import winterwell.jtwitter.Status; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

@SuppressWarnings("unused") 
public class StatusData { 
    static final String TAG = "StatusData"; 
    public static final String DB_NAME = "timeline.db"; 
    public static final int DB_VERSION = 1; 
    public static final String TABLE = "status"; 
    public static final String C_ID = "_id"; 
    public static final String C_CREATED_AT = "created_at"; 
    public static final String C_USER = "user_name"; 
    public static final String C_TEXT = "status_text"; 

    Context context; 
    DbHelper dbHelper; 
    SQLiteDatabase db; 

    public StatusData(Context context) { 
     this.context = context; 
     dbHelper = new DbHelper(); 
    } 

    public void insert(Status status) { 
     db = dbHelper.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 

     values.put(C_ID, status.id.toString()); 
     values.put(C_CREATED_AT, status.createdAt.getTime()); 
     values.put(C_USER, status.user.name); 
     values.put(C_TEXT, status.text); 
     db.insert(TABLE, null, values); 
     db.insertWithOnConflict(TABLE, null, values, 
       SQLiteDatabase.CONFLICT_IGNORE); 
    } 

    class DbHelper extends SQLiteOpenHelper { 

     public DbHelper() { 
      super(context, DB_NAME, null, DB_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      String sql = String.format("Create Table : %s " 
        + "(%s int Primary Key, " + "%s int, %s text, %s text)", 
        TABLE, C_ID, C_CREATED_AT, C_USER, C_TEXT); 
      Log.d(TAG, "Oncreate with SQL : " + sql); 
      db.execSQL(sql); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // Usually ALTER the TABLE 
      db.execSQL("drop if exists " + TABLE); 
      onCreate(db); 
     } 

    }