简体   繁体   English

在ListView中显示数据库中的数据

[英]Display data from Database in ListView

So I've been reading the Android Developer docs and have spent the last few hours reading tutorials online and have downloaded a few samples. 因此,我一直在阅读Android Developer文档,并花了最后几个小时在线阅读教程并下载了一些示例。 Some of them compile, some don't. 其中有些可以编译,有些则不能。

The ones that compile use the SQLLiteDatabase in different ways than other samples I downloaded. 与我下载的其他示例相比,编译该示例的方式使用SQLLiteDatabase的方式不同。 I'm now totally confused. 我现在很困惑。 I was following along with the Training docs on http://developer.android.com/guide/topics/data/data-storage.html#db until I got half way down the page where it just stopped, and said that we can checkout the Note Pad sample. 我一直在跟踪http://developer.android.com/guide/topics/data/data-storage.html#db上的Training文档,直到页面停了下来的一半,并说我们可以检查记事本示例。 But the Notepad sample uses different code than what I had just read in the docs. 但是记事本示例使用的代码与我在文档中刚刚阅读的代码不同。

My scenario: 我的情况:

  • Display ListView that pulls data from SQLiteDatabase in MainActivity.xml 显示ListView,该视图从MainActivity.xml中的SQLiteDatabase中提取数据
  • If user presses 'Add entry' button in AddEntryActivity.xml, add the entry to the database 如果用户按下AddEntryActivity.xml中的“添加条目”按钮,则将条目添加到数据库中
  • If user presses 'Delete entry' button in DeleteEntryActivity.xml, delete the entry in the database. 如果用户按下DeleteEntryActivity.xml中的“删除条目”按钮,则删除数据库中的条目。

So it's pretty simple stuff which I could do in just a couple minutes in C# but I'm new to android and I have absolutely no idea where to start. 因此,这是非常简单的事情,我可以在C#中仅用几分钟就可以完成,但是我是android的新手,所以我绝对不知道从哪里开始。 I started with the documentation, but all it does it go about half way and then refers you to the sample which is just more confusing since it uses different code. 我从文档开始,但是它所做的一切只进行了一半,然后将您引向示例,因为该示例使用了不同的代码,因此更加令人困惑。

How can I achieve this? 我该如何实现?

Use SQLiteDatabase class for this 为此使用SQLiteDatabase类

     SQLiteDatabase db = openOrCreateDatabase("Database_Name", MODE_PRIVATE, null);

     //Create Table 
   String  strsql = "CREATE TABLE IF NOT EXISTS TableName(Name  VARCHAR(30),id    INT(15)  unique) ";
db.execSQL(strsql);       

you can fire query as you do in MYSQL(INSERT,UPDATE,DELETE) using db.execSQL() 您可以使用db.execSQL()像在MYSQL(INSERT,UPDATE,DELETE)中一样触发查询

To Retrive data you can use this 要检索数据,您可以使用此

   strsql = "SELECT * FROM TableName ";

   Cursor c = db.rawQuery(strsql, null);

        int count = c.getCount();
        c.moveToFirst();

        while (k >= 0) {
                  ListviewContent.add(c.getString(c.getColumnIndex("Name")));
            setListAdapter(new ListViewAdapter(this));
            c.moveToNext();
            k--;

        }

   //you can set retrived data to list view as shown above.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在带有片段的Lis​​tView中显示数据库中的数据 - Display Data from Database in ListView With Fragment 如何显示从数据库表中获取的数据到ListView中? - How to display data fetched from the database table into listview? 在ListView中显示来自sqlite数据库的数据,而不是Toast? - Display data from sqlite database in listview instead of toast? 在列表视图中显示数据库中的数据并删除选中的项目 - Display data from database in listview and delete checked item 使用Parse.com从数据库中获取数据以显示在列表视图中 - Fetch data from database to display in a listview using Parse.com 如何从数据库中检索数据并在具有多个复选框的列表视图中显示? - How to Retrive Data From The Database and Display in listview with multiple checkboxes? 具有来自外部数据库的数据的ListView - ListView with data from an external database 如何将数据库中的值显示到列表视图中 - How to display values from a database into a listview 我正试图将数据从Firebase数据库显示到ListView,但是在创建Listadapter时会出错 - I am tring to display data from firebase database to listview but getting eror when I create Listadapter 如何使用 ArrayAdapter 从 Firebase 数据库读取数据并在 ListView 中显示多个字段 - How to Read Data from Firebase Database and Display Multiple Fields in ListView with ArrayAdapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM