【问题标题】:Showing database in ListView在 ListView 中显示数据库
【发布时间】:2012-01-25 08:09:37
【问题描述】:

我是 android 新手,在学习了很多教程后,我仍然无法获得东西。

我已经创建了一个数据库,现在正在读取这个数据库。 这个数据库和 3 列都是三个字符串类型。

现在我想用 ListView 显示这个数据库。我有这个ListView 在R.layout.viewDatabase 中使用ID“listDatabaseItems”创建

目前我的代码是:

Cursor c = sqliteDatabase.query(MY_DATABASE_TABLE, null, null, null, null, null, null);

int firstNameColumn = c.getColumnIndex("name_column");
int protectedColumn = c.getColumnIndex("protected_id_column");

ArrayList<String> results = new ArrayList<String>();
int i = 0;

c.moveToFirst();
do {
    i++;
    String firstName = c.getString(firstNameColumn);
    String protectedID = c.getString(protectedColumn);
    results.add(firstName + protectedID); // NEED TO add here 
} while (c.moveToNext()); 

sqliteDatabase.close();

请告诉我我做了哪些更改以及如何在 ListView 中显示我的数据库。

最好的问候。

【问题讨论】:

  • 请正确提及您面临的问题...?
  • 我的代码缺少详细信息,如何将结果数组链接到我的 ListView。
  • 我认为您在遍历光标时遇到了问题...??
  • 不,我只是想知道如何使用 ListView 显示光标中存储的内容

标签: java android eclipse android-emulator


【解决方案1】:

创建一个Adapter 将您的ListArray 值添加到其中,然后将setAdapter 简单地添加到您的列表视图中,

在你的代码之后,只添加两行,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results);
mListView.setAdapter(adapter);

另外,你的问题对于安卓来说听起来很基础所以看看这些链接..

SimpleCursorAdapters and ListViews

Android SQLite Database and ContentProvider - Tutorial

Filling the Layout with Data

【讨论】:

  • 语法错误。它不接受结果。你确定 ArrayAdapter?
  • mListView.setAdapter(adapter);(其中 mListView 是一个列表视图的实例,而不是 ListView 类)——或者,如果你在 ListActivity 的子类中,你可以使用 getListView().setAdapter(adapter);
  • 感谢您的帮助,它成功了,但是我怎样才能将数据列出到不同的列中,而不是将它们列在一行中?
  • 欢迎哥们,看Multicolumn ListView in Android..它的多列listview..
【解决方案2】:

由于您首先使用的是光标,所以我实际上会使用 SimpleCursorAdapter。 CursorAdapter 允许您避免将整个数据集加载到内存中,从而加快初始加载时间并避免对大型数据集进行大量分配。

要扩展本文 (SimpleCursorAdapters and ListViews) 中的基本设置,您还需要设置一个视图绑定器来自定义数据绑定到视图的方式,即。连接你的两个字符串。

类似这样的:

SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
          //you're only using one view, so we can ignore columnIndex
          TextView text = view.findViewById(R.id.list_item_text);

          //the cursor is already in position for this list item
          int firstNameColumn = c.getColumnIndex("name_column");
          int protectedColumn = c.getColumnIndex("protected_id_column");
          String firstName = c.getString(firstNameColumn);
          String protectedID = c.getString(protectedColumn);
          text.setText(firstName+protectedID);
          return true; //we have handled binding the data
      }
 };

然后,当您设置适配器时,您会向它传递一个可用于每个项目的布局。布局应包含一个 TextView 来保存您要显示的一个文本项。您可以按照 user370305 的建议使用 android.R.layout.simple_list_item_1,但我不确定它的 TextView 的 id 是什么——它可能是 android.R.id.text1。

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
    context,           //can be your Activity 
    R.layout.list_item,  //xml layout containing textview with id 'list_item_text'
    c,                 //the cursor
    new String[] {"name_column"}, //the column to read (we'll do more in the binder)
    new int[] {R.id.list_item_text}, //the view to hold the data
    0 //no flags
);
adapter.setViewBinder(binder);    

以同样的方式将其添加到您的 ListView 中:

ListView listView = //your ListView reference
listView.setAdapter(adapter);

//or in a ListActivity:
setListAdapter(adapter);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多