Android 应用程序,ListView 中的条件文本

发布于 2024-11-02 22:21:18 字数 526 浏览 4 评论 0原文

我有一个使用 SimpleCursorAdapter 从 SQLite 数据库填充的列表视图。光标中返回的列之一是整数值 0 或 1。在我的列表视图中,我想以更友好的形式(即“是”或“否”)显示它,并且可能使用不同的文本颜色对于每个。这是我的来源:

Cursor c = dbHelper.fetchAllItems();
startManagingCursor(c);

String[] from = {"deployed", "designation", "serial"};
int[] to = {R.id.deployed, R.id.designation, R.id.serial};

setListAdapter(new SimpleCursorAdapter(this, R.layout.list_item, c, from, to));

当 SimpleCursorAdapter 只是将每个视图映射到列名称时,我将如何有条件地切换布局中的元素和/或属性。 (可以安全地假设我不能使用 SimpleCursorAdapter 来完成此操作吗?)

I have a list view that is being populated from an SQLite database using the SimpleCursorAdapter. One of the columns being returned in the cursor is an integer value 0 or 1. In my list view, I would like to display this in a more friendly form (ie. "Yes" or "No") and possibly with different text colors for each. Here is my source:

Cursor c = dbHelper.fetchAllItems();
startManagingCursor(c);

String[] from = {"deployed", "designation", "serial"};
int[] to = {R.id.deployed, R.id.designation, R.id.serial};

setListAdapter(new SimpleCursorAdapter(this, R.layout.list_item, c, from, to));

How would I conditionally switch elements and/or properties in the layout when the SimpleCursorAdapter simply maps each view to a column name. (Is it safe to assume I can't use SimpleCursorAdapter to accomplish this?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£烟消云散 2024-11-09 22:21:18

通过添加自定义适配器、扩展 CursorAdapter 解决

:修改:

Cursor c = dbHelper.fetchAllItems();
startManagingCursor(c);

setListAdapter(new RowAdapter(this, c));

新建嵌套类:

private static class RowAdapter extends CursorAdapter {

    public RowAdapter(Context context, Cursor c) {
        super(context, c);
    }

    public void bindView(View view, Context context, Cursor c) {
        TextView vDesignation = (TextView) view.findViewById(R.id.designation);
        TextView vSerial = (TextView) view.findViewById(R.id.serial);
        TextView vDeployed = (TextView) view.findViewById(R.id.deployed);

        String designation = c.getString(c.getColumnIndexOrThrow("designation"));
        String serial = c.getString(c.getColumnIndexOrThrow("serial"));
        int deployed = c.getInt(c.getColumnIndexOrThrow("deployed"));

        vDesignation.setText(designation);
        vSerial.setText(serial);
        vDeployed.setText(deployed > 0 ? R.string.yes : R.string.no);
        vDeployed.setTextColor(deployed > 0 ? view.getResources().getColor(R.color.yes) : view.getResources().getColor(R.color.no));
    }

    public View newView(Context context, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.list_item, parent, false);
        bindView(view, context, c);
        return view;
    }
}

Solved by adding a custom adapter, extending CursorAdapter

Modification:

Cursor c = dbHelper.fetchAllItems();
startManagingCursor(c);

setListAdapter(new RowAdapter(this, c));

New nested class:

private static class RowAdapter extends CursorAdapter {

    public RowAdapter(Context context, Cursor c) {
        super(context, c);
    }

    public void bindView(View view, Context context, Cursor c) {
        TextView vDesignation = (TextView) view.findViewById(R.id.designation);
        TextView vSerial = (TextView) view.findViewById(R.id.serial);
        TextView vDeployed = (TextView) view.findViewById(R.id.deployed);

        String designation = c.getString(c.getColumnIndexOrThrow("designation"));
        String serial = c.getString(c.getColumnIndexOrThrow("serial"));
        int deployed = c.getInt(c.getColumnIndexOrThrow("deployed"));

        vDesignation.setText(designation);
        vSerial.setText(serial);
        vDeployed.setText(deployed > 0 ? R.string.yes : R.string.no);
        vDeployed.setTextColor(deployed > 0 ? view.getResources().getColor(R.color.yes) : view.getResources().getColor(R.color.no));
    }

    public View newView(Context context, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.list_item, parent, false);
        bindView(view, context, c);
        return view;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文