2011-12-11 63 views
0

我回來了關於Android數據庫的另一個問題。如何顯示Android數據庫內容?

我正在存儲一個簡單的字符串到我的android數據庫,然後希望顯示所有的條目列表。但是,該列表在我的模擬器(或我的實際設備)上沒有顯示任何內容,LogCat也沒有(將條目保存到數據庫或調用它們時)。

下面你有我的代碼

  • 保存項功能:

    public long createEntry(String entry){ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_ENTRY, entry); 
    
    return db.insert(DATABASE_TABLE, null, initialValues); 
    } 
    
  • 的獲取條目功能:

    public Cursor fetchAllEntries(){ 
    return db.query(DATABASE_TABLE,new String [] {KEY_ROW_ID, KEY_ENTRY}, null, null, null, null, null); 
    } 
    
  • 列表活動:

    entryList = (ListView)findViewById(R.id.recordsList); 
    Cursor c = dbAdapter.fetchAllEntries(); 
    
    if(c!=null){ 
        startManagingCursor(c); 
        String [] from = new String [] {PirelliDbAdapter.KEY_ENTRY}; 
        int [] to = new int [] {R.id.text1}; 
    
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.entryrow, c, from, to); 
        entryList.setAdapter(adapter); 
    } 
    
    dbAdapter.close(); 
    
  • 最後名單活動的XML:

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    
    <ListView 
    android:id="@+id/recordsList" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
    
    <TextView 
    android:id="@+id/textViewList" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
    </LinearLayout> 
    

非常感謝您抽出時間來看看在這個和幫助我。我真的難住,不知道從哪裏走...

回答

0

你的代碼沒有問題,請你檢查數據庫文件天氣數據是有一種方法是使用sqlite瀏覽器(或)通過命令提示符 {$ adb shell $ cd data/data/projectname/databases/
檢查您的數據庫文件是否已創建,然後轉到 - > sqlite3數據庫文件名您將找到sqlite提示符 - 因此你可以探索。表和名稱*

上述步驟提到鑑於新生,如果你知道這個更早的請忽略

來探索db文件。

請檢查該https://stackoverflow.com/questions/2149438/tool-to-see-android-database-tables-and-data

+0

這個鏈接是死 – Ixx

1

首先,檢查你的數據庫中創建或者沒有,那麼檢查是否產生或不表,通過命令行。如果所有的東西都OK,那麼嘗試打印光標來檢查它是否從數據庫中檢索數據。打印光標值使用下面的邏輯..

*if(!cursor.isAfterLast()) 
    { 
     cursor.moveToFirst(); 
     do{ 
      String entry = cursor.getString(cursor.getColumnIndex(KEY_ENTRY)); 
      Log.v("value:",entry); 
      cursor.moveToNext(); 
      }while(!cursor.isAfterLast());* 
+0

+1 ...尼斯答案:d – mAc

+0

嘿帕文,我已經看到你的答案,但是這不是我的幫助。我也試圖從數據庫中檢索數據並將其顯示在TextView上。這是我的問題的鏈接。你可以幫我嗎。 http://stackoverflow.com/questions/12367369/android-how-to-show-database-entries-onclick-event-on-the-textview – Anupam