2012-10-05 47 views
0

Im在製作應用程序的過程中。我的第一個活動顯示幾個按鈕。每個按鈕應該從數據庫中的一個表中讀出數據。我有資產文件夾內的數據庫,並且還將android元數據和_id添加到數據庫。在我的主要活動中,我用以下代碼「導入」db:SQlite數據庫有幾個表填充到不同的列表視圖

public class DataBaseHelper extends SQLiteOpenHelper{ 



    private static final String DB_PATH = "/data/data/com.mypackage/databases/"; 

    private static final String DB_NAME = "dbname.sqlite"; 

    private SQLiteDatabase myDataBase; 

    private final Context myContext; 

    /** 
     * Constructor 
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources. 
     * @param context 
     */ 
    public DataBaseHelper(Context context) { 

    super(context, DB_NAME, null, 1); 
    this.myContext = context; 
    } 

    /** 
     * Creates a empty database on the system and rewrites it with your own database. 
     * */ 
    public void createDataBase() throws IOException{ 

    boolean dbExist = checkDataBase(); 

    if(dbExist){ 
    //do nothing - database already exist 
    }else{ 

    //By calling this method and empty database will be created into the default system path 
    //of your application so we are gonna be able to overwrite that database with our database. 
    this.getReadableDatabase(); 

    try { 

    copyDataBase(); 

    } catch (IOException e) { 

    throw new Error("Error copying database"); 

    } 
    } 

    } 

    /** 
     * Check if the database already exist to avoid re-copying the file each time you open the application. 
     * @return true if it exists, false if it doesn't 
     */ 
    private boolean checkDataBase(){ 

    SQLiteDatabase checkDB = null; 

    try{ 
    String myPath = DB_PATH + DB_NAME; 
    checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

    }catch(SQLiteException e){ 

    //database does't exist yet. 

    } 

    if(checkDB != null){ 

    checkDB.close(); 

    } 

    return checkDB != null ? true : false; 
    } 

    /** 
     * Copies your database from your local assets-folder to the just created empty database in the 
     * system folder, from where it can be accessed and handled. 
     * This is done by transfering bytestream. 
     * */ 
    private void copyDataBase() throws IOException{ 

    //Open your local db as the input stream 
    InputStream myInput = myContext.getAssets().open(DB_NAME); 

    // Path to the just created empty db 
    String outFileName = DB_PATH + DB_NAME; 

    //Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(outFileName); 

    //transfer bytes from the inputfile to the outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer))>0){ 
    myOutput.write(buffer, 0, length); 
    } 

    //Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 

    } 

    public void openDataBase() throws SQLException{ 

    //Open the database 
    String myPath = DB_PATH + DB_NAME; 
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

    } 

    @Override 
    public synchronized void close() { 

    if(myDataBase != null) 
    myDataBase.close(); 

    super.close(); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 

上面的代碼是否足夠?數據庫是否可用於其他活動?

從我的主要活動,現在我想點擊按鈕,例如「植物」。這應該將我重定向到下一個活動,並使用DB的植物表格中的所有植物名稱創建一個列表視圖。

重定向我與下面的代碼做:

Button plantsBtn = (Button) findViewById(R.id.button5); 

    plantsBtn.setOnClickListener(new View.OnClickListener() { 


       public void onClick(View view) { 
     //    Intent myIntent = new Intent(view.getContext(), plantslist.class); 
        startActivityForResult(myIntent, 0); 
       } 



    }); 

這裏是IM卡住。我不知道如何只用名字來填充列表視圖。同樣在點擊列表視圖中的工廠名稱後,我想要轉到另一個線性佈局,該佈局顯示所點擊的特定工廠的同一表格中其他列的所有其他詳細信息。

數據庫裏面有很多表與例如「汽車」,將遵循同樣的原則,但我想如果我能得到它的工作爲一個表,我能夠使其適應自己的人。

我知道有很多的教程在那裏了,不過我還沒有真正找到那個讓我明白或適應我的情況。幫助將不勝感激!

+0

它是什麼,你不明白嗎?活動?活動之間的相互作用?佈局?數據庫? –

+0

創建一個DataBaseHandler類,通過它可以訪問數據庫表並檢索所需的數據。例如,使用查詢如「從植物表中選擇*」並將與每行相關的數據存儲在植物bean類中。將bean的集合傳遞給列表適配器。並使用bean類的getName方法顯示名稱。單擊時,傳遞相應的bean並使用bean對象的字段值填充線性佈局。 –

+0

@CL:我不明白如何填充列表視圖中只有來自幾個表之一的名稱列中的數據。 – SunnySonic

回答

1

要使myDataBase可從所有活動中獲得,您可以製作自己的應用程序類,並將其擴展爲Application並在此處聲明。然後,您可以使用getter從您的活動中檢索它。

關於你想用數據庫中的數據完成什麼,我認爲你應該看看CursorsCursorAdapter(如果你還沒有的話)。下面是如何與Loaders使用它一個很好的解釋:http://developer.android.com/guide/components/loaders.html

你可以找到許多教程與您的朋友谷歌:)

+0

謝謝。我會仔細看看的。 – SunnySonic

相關問題