【问题标题】:Android: Read integer as string in a ListViewAndroid:在 ListView 中将整数读取为字符串
【发布时间】:2014-04-04 02:07:59
【问题描述】:

我目前有一个包含从 sqlite 数据库填充的 listView 的应用程序。 ListView 包含每个项目的两个文本视图,一个用于名称,一个用于状态。状态由 0 或 1 显示,例如 true 或 false。我想让每个包含数字 0 或 1 的项目作为状态显示为打开或关闭。

列表视图的方法:

// Return all data in the database.
    public Cursor getAllRows() {
        String where = null;
        Cursor c = ourDatabase.query(true, DATABASE_TABLE, ALL_KEYS, where,
                null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    // also added
    // Get a specific row (by rowId)
    public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c = ourDatabase.query(true, DATABASE_TABLE, ALL_KEYS, where,
                null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

这是我的 listView 代码:

private void loadListView() {
    // TODO Auto-generated method stub
    Locker getList = new Locker(this);
    getList.open();
    Cursor cursor = getList.getAllRows();
    startManagingCursor(cursor);
    // setup mapping
    String[] fromFieldNames = new String[] { getList.KEY_LOCKERNUMBER,
            getList.KEY_STATUS };

    int[] toViewIDs = new int[] { R.id.tvLockerNumber, R.id.tvSatus };

    // create adapter
    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
            R.layout.itemlayout, cursor, fromFieldNames, toViewIDs);

    // set the adapter
    ListView myList = (ListView) findViewById(R.id.lvInfo);
    myList.setAdapter(myCursorAdapter);
}

我该怎么做?

【问题讨论】:

  • "我想让每个包含数字 0 或 1 的项目作为状态显示为打开或关闭。"你说的开放还是封闭是什么意思?您是在谈论不同的布局吗?
  • 目前在列表视图中,它从我的数据库中获取状态变量,它可以是 0 或 1。我希望我的列表视图能够解释这些整数并在 textview 上显示为打开或关闭。

标签: java android sqlite listview android-listview


【解决方案1】:

您必须实现 myCursorAdapter 的 setViewValue 方法,以便您可以将 textview 设置为 1 或 0 时打开或关闭。

this answer 为例。另外,请查看developer's guide 了解更多详情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 2020-01-06
    • 2013-02-13
    • 2017-03-19
    • 1970-01-01
    相关资源
    最近更新 更多