sqlite - 安卓 : Populating a GridtView from the SQLite Database

标签 sqlite gridview

Gridview 在将数据保存到数据库时不会填充 Sqlite 数据库中的任何数据。 Logcat 也没有产生任何错误。

我的数据库是。

public Cursor getAllRows() {
  SQLiteDatabase db = this.getReadableDatabase();
  //String where = null;
  Cursor c = db.rawQuery("SELECT  * FROM " + DATAALL_TABLE, null);
  if (c != null) {
    c.moveToFirst();
  }
  return c;
}

主要事件。
public void populateListView() {

  Cursor cursor = db.getAllRows();
  String[] fromFieldNames = new String[] {
    DBHelper.COURSES_KEY_FIELD_ID1, DBHelper.FIELD_MATERIALDESC, DBHelper.FIELD_MATERIALNUM
  };
  int[] toViewIDs = new int[] {
    R.id.textView1, R.id.textView3, R.id.textView2
  };
  SimpleCursorAdapter myCursorAdapter;
  myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), android.R.layout.activity_list_item, cursor, fromFieldNames, toViewIDs, 0);
  GridView myList = (GridView) findViewById(R.id.gridView1);
  myList.setAdapter(myCursorAdapter);

}

发帖给尊敬的成员@MikeT 建议其工作正常但需要对齐,

原样

enter image description here

预期格式

enter image description here

最佳答案

您的问题,假设表中有数据,很可能 R.id.textView1 .... 3 与传递给 SimpleCursorAdapter 的布局无关。即您的问题可能与传递给 SimpleCursorAdapter 的布局和作为 传递的 View 的 Id 的组合有关。至身份证。

如果您要使用:-

    gridview = this.findViewById(R.id.gridView1);
    csr = DBHelper.getAllRows();
    myCursorAdapter = new SimpleCursorAdapter(
            getBaseContext(),
            //android.R.layout.simple_list_item_2,
            android.R.layout.activity_list_item,
            csr,
            new String[]{
                    SO50674769DBHelper.COURSES_KEY_FIELD_ID1
                    //SO50674769DBHelper.FIELD_MATERIALDESC,
                    //SO50674769DBHelper.FIELD_MATERIALNUM
            },
            new int[]{android.R.id.text1}, //<<<< ID of the available view
            0
    );
    gridview.setAdapter(myCursorAdapter);

那么结果将是: -

enter image description here

更改为使用不同的库存布局和 2 个字段:-
    gridview = this.findViewById(R.id.gridView1);
    csr = DBHelper.getAllRows();
    myCursorAdapter = new SimpleCursorAdapter(
            getBaseContext(),
            android.R.layout.simple_list_item_2,
            //android.R.layout.activity_list_item, //<<<< Changed Layout
            csr,
            new String[]{
                    SO50674769DBHelper.COURSES_KEY_FIELD_ID1,
                    SO50674769DBHelper.FIELD_MATERIALDESC,
                    //SO50674769DBHelper.FIELD_MATERIALNUM
            },
            new int[]{android.R.id.text1, android.R.id.text2}, //<<<< ID's of the 2 views
            0
    );
    gridview.setAdapter(myCursorAdapter);
  • 注意 DatabaseHelper 类为方便起见如此命名。

  • 会导致:-

    enter image description here

    因此,我怀疑您需要将布局更改为您自己的布局。

    此外,正如您的 getAllRows 所暗示的那样方法一点都不理想。
  • 检查空游标是没有用的,因为从 rawQuery 返回的游标不会为空。在这种情况下,光标 getCount 可能为空。方法将返回 0 ()。
  • moveToFirst 也是一种浪费。

  • 只需:-
    public Cursor getAllRows() {
      SQLiteDatabase db = this.getReadableDatabase();
      return db.rawQuery("SELECT  * FROM " + DATAALL_TABLE, null);
    }
    

    关于sqlite - 安卓 : Populating a GridtView from the SQLite Database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50674769/

    相关文章:

    Android eclipse 将 SQLite 表行转储到文本文件中

    android - 用户输入的撇号使 android 应用程序崩溃(与 SQL 相关)

    java - 插入大量记录

    mysql - 如何在不编写与子查询相同的查询的情况下在 WHERE 子句中使用实际行数 (COUNT(*))?

    c# - 在 Gridview 中选择行+1?

    c++ - Sqlite3 Select 查询慢,而插入/更新/删除没有问题

    asp.net - 在asp.net中显示按月和年分组的记录

    c# - 如何将 gridview 中的列的总和发送到 gridview 外部的文本框?

    c# - 选择下拉列表时无法阻止 gridview 刷新

    c# - 排序重定向到另一个页面