在Android上使用的ListView从SQLite数据库查看数据 [英] View data from SQLite database using ListView on Android

查看:196
本文介绍了在Android上使用的ListView从SQLite数据库查看数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的Andr​​oid开发。我设法保存到SQLite数据库的数据。现在,我要的是当我打电话可视数据来查看这些数据()。我有可视数据(),它显示的数据作为敬酒,我做它作为一个样本。现在,我需要这些数据用一个ListView上了一个新的活动来展示,但数据显示的数量取决于有多少数据是目前数据库中,如果用户保存的10个项目,然后我希望所有的10个项目,以显示向上。我该怎么办呢?

I am quite new to Android development. I managed to get data saved to SQLite database. Now, what I want is to view these data when I call viewData(). I have viewData() which shows data as a Toast as I made it as a sample. Now I need these data to show on a new activity using a ListView, but the number of data to show is depending on how many data is in the database at the moment, If user saved 10 items then I want all the 10 items to shown up. How can I do it?

我希望我的问题是清楚的。

I hope my question is clear.

先谢谢了。

推荐答案

我会尽量给一个深入的答案。

I will try to give an in-depth answer to this.

每当你想获取并从数据库中显示的数据列表,您可以使用的ListView GridView控件微调,等吧。

Whenever you want to fetch and display a list of data from the database, you can use a ListView, GridView, Spinner, etc for it.

您可以使用的CursorAdapter ,它可以使查询和显示的数据更简单和容易的工作。

You can use a CursorAdapter which can make the job of querying and displaying data much more simple and easy.

下面是它的一个基本的视觉重新presentation,

Here is a basic visual representation of it,

在这里输入的形象描述

第1步

首先,你需要创建一个数据库。正如你提到的问题,很显然,你知道如何创建一个数据库,并把一些数据进去。所以我不打算进入它的深处。

Firstly, you need to create a database. As mentioned in your question, it is clear that you know how to create a database and put some data into it. So I am not going into the depths of it.

第2步

我们需要定义布局在<$​​ C $ C>的ListView 用于单个项目,并将其保存为 RES /布局/ item_todo.xml 这仅仅是一个样品的布局,你可以设计你想​​要的任何种类的布局。

We need to define the layout to be used for the individual items in the ListView and save it as res/layout/item_todo.xml This is just a sample layout, you can design any kind of layout you want to.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/tvBody"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Study cursors"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/tvPriority"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="3"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

第3步

现在,我们需要定义一个适配器。在这里,我们使用的是的CursorAdapter ,其中转换的光标(您提供)为视图(由布局定义)。

Now we need to define an adapter. Here we are using a CursorAdapter which converts a Cursor (that you provide) into Views (defined by your layout).

有两种方法, NewView的 bindView 这是我们需要重写。的NewView的负责充气newViews首次和bindView是负责将数据绑定到视图

There are two methods, newView and bindView which we need to override. The newView is responsible for inflating newViews for the first time and the bindView is responsible for binding the data to the Views.

public class TodoCursorAdapter extends CursorAdapter {
  public TodoCursorAdapter(Context context, Cursor cursor) {
      super(context, cursor, 0);
  }

  // The newView method is used to inflate a new view and return it, 
  // you don't bind any data to the view at this point. 
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      return LayoutInflater.from(context).inflate(R.layout.item_todo, parent, false);
  }

  // The bindView method is used to bind all data to a given view
  // such as setting the text on a TextView. 
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
      // Find fields to populate in inflated template
      TextView tvBody = (TextView) view.findViewById(R.id.tvBody);
      TextView tvPriority = (TextView) view.findViewById(R.id.tvPriority);
      // Extract properties from cursor
      String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
      int priority = cursor.getInt(cursor.getColumnIndexOrThrow("priority"));
      // Populate fields with extracted properties
      tvBody.setText(body);
      tvPriority.setText(String.valueOf(priority));
  }
}

第四步

现在,你可以清楚地看到,该构造需要一个上下文光标。现在,我们需要查询数据库和检索数据转换成光标并把它传递到适配器。

Now as you can clearly see, that the constructor needs a Context and a Cursor. Now we need to query the database and retrieve the data into a Cursor and pass it to the adapter.

// TodoDatabaseHandler is a SQLiteOpenHelper class connecting to SQLite
TodoDatabaseHandler handler = new TodoDatabaseHandler(this);
// Get access to the underlying writeable database
SQLiteDatabase db = handler.getWritableDatabase();
// Query for items from the database and get a cursor back
Cursor todoCursor = db.rawQuery("SELECT  * FROM todo_items", null);

第5步

这是我们需要实例化的适配器和连接的最后一步的ListView 通过适配器来填充数据。

This is the last step where we need to instantiate the adapter and attach the ListView with the adapter to populate the data.

// Find ListView to populate
ListView lvItems = (ListView) findViewById(R.id.lvItems);
// Setup cursor adapter using cursor from last step
TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, todoCursor);
// Attach cursor adapter to the ListView 
lvItems.setAdapter(todoAdapter);

这篇关于在Android上使用的ListView从SQLite数据库查看数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆