【问题标题】:How to display Android database content?如何显示Android数据库内容?
【发布时间】:2011-12-11 15:59:36
【问题描述】:

我又问了一个关于 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();
    
  • 最后是 List Activity 的 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>
    

非常感谢您抽出宝贵时间查看并帮助我。我真的很难过,不知道从哪里开始......

【问题讨论】:

    标签: android database sqlite list listview


    【解决方案1】:

    首先,检查您的数据库是否已创建,然后通过命令行检查表是否已创建。如果一切正常,则尝试打印光标以检查它是否从数据库中检索数据。打印光标值使用以下逻辑..

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

    【讨论】:

    • 嘿帕万,我已经看到你的回答,但这对我没有帮助。我还试图从数据库中检索数据并将其显示在 TextView 上。这是我的问题的链接。你能帮助我吗。 stackoverflow.com/questions/12367369/…
    【解决方案2】:

    您的代码没有问题,请您检查数据库文件天气数据是否存在一种方法是使用 sqlite 浏览器(或)在命令提示符下执行此操作 {$ adb shell $ cd data/data/projectname/databases/
    检查您的 db 文件是否已创建,然后转到 --> sqlite3 数据库文件名,您将找到 sqlite 提示符-因此您可以作为 .tables 进行探索并从表名中选择 * }

    以上步骤是针对新手的,如果早知道请忽略

    探索 db 文件。

    请查看https://stackoverflow.com/questions/2149438/tool-to-see-android-database-tables-and-data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 2012-09-07
      • 2019-01-08
      • 1970-01-01
      相关资源
      最近更新 更多