2011-12-11 116 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