可以向 SimpleCursorAdapter 添加附加列

发布于 2024-10-18 03:48:39 字数 2169 浏览 4 评论 0原文

我已成功使用 SimpleCursorAdapter 列出显示名称和值(均在我的数据库中)并将它们显示在我的列表活动中。

我现在想做的是为列表活动中的每个项目添加一个新视图(ImageView)。最后应该看起来像这样。

Image_1_NotInDB -- DisplayName1FromDB -- DisplayName1FromDB
Image_2_NotInDB -- DisplayName2FromDB -- DisplayName2FromDB.

图像将会有所不同(基于 DisplayName1FromDB)。我认为 SimpleCursorAdapter 不再适合此目的。

我尝试创建一个扩展SimpleCursorAdapter的customSimpleCursorAdapter,并尝试使用“newView”和“bindView”方法来实现。我几乎遵循了这个:自定义 CursorAdapters

问题是;我使用的图像基于数据库中的值(我打算在构造函数或 customSimpleCursorAdapter 中传递该值)

public View newView(Context pContext, Cursor pCursor, ViewGroup pParent)
{
    Cursor lCursor = getCursor();
    final LayoutInflater inflater = LayoutInflater.from(pContext);
    View lView = inflater.inflate(layout, pParent, false);
    int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
    // "variable" is a member variable (set at the constructor)
    ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
    if (lImageView != null)
    {
        lImageView.setImageResource(lImage);
    }
    return pParent;
}

public void bindView(View pView, Context pContext, Cursor pCursor)
{
    int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
    // "variable" is a member variable (set at the constructor)
    ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
    if (lImageView != null)
    {
        lImageView.setImageResource(lImage);
    }
}

这就是我尝试使用“customSimpleCursorAdapter”的方法

    String[] lDisplay = new String[] {KEY_NAME, KEY_TIME};
    int[] lValues = new int[] {R.id.name, R.id.time};
    CustomRowCursorAdapter lCursorAdapter = new CustomRowCursorAdapter(this, R.layout.row, lSTime, lDisplay, lValues, "MY IDEA WAS TO PASS THE ANIMAL NAME HERE, BUT NOT LUCK as I am not sure How ");
    lCursorAdapter.newView(this, lSTime, getListView());
    lCursorAdapter.bindView(getListView(), this, lSTime);
    setListAdapter(lCursorAdapter);

ArrayAdapter 是答案吗?如果是,您能分享一下您将传递给它的参数吗?

I have successfully used a SimpleCursorAdapter to list displayname and value (both in my DB) and displayed them in my list activity.

What I am was trying to do now was add a new view (ImageView) for each item in my list activity. It is supposed to look like this in the end.

Image_1_NotInDB -- DisplayName1FromDB -- DisplayName1FromDB
Image_2_NotInDB -- DisplayName2FromDB -- DisplayName2FromDB.

The image is going to different (based on DisplayName1FromDB). I don't think SimpleCursorAdapter is good anymore for this purpose.

I tried creating a customSimpleCursorAdapter extending SimpleCursorAdapter and tried to use 'newView' and 'bindView' methods to achieve. I pretty much followed this: Custom CursorAdapters.

The problem is; what Image I use is based on a value from DB (which I intended to pass in the constructor or customSimpleCursorAdapter)

public View newView(Context pContext, Cursor pCursor, ViewGroup pParent)
{
    Cursor lCursor = getCursor();
    final LayoutInflater inflater = LayoutInflater.from(pContext);
    View lView = inflater.inflate(layout, pParent, false);
    int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
    // "variable" is a member variable (set at the constructor)
    ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
    if (lImageView != null)
    {
        lImageView.setImageResource(lImage);
    }
    return pParent;
}

public void bindView(View pView, Context pContext, Cursor pCursor)
{
    int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
    // "variable" is a member variable (set at the constructor)
    ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
    if (lImageView != null)
    {
        lImageView.setImageResource(lImage);
    }
}

This is how I tried using the "customSimpleCursorAdapter"

    String[] lDisplay = new String[] {KEY_NAME, KEY_TIME};
    int[] lValues = new int[] {R.id.name, R.id.time};
    CustomRowCursorAdapter lCursorAdapter = new CustomRowCursorAdapter(this, R.layout.row, lSTime, lDisplay, lValues, "MY IDEA WAS TO PASS THE ANIMAL NAME HERE, BUT NOT LUCK as I am not sure How ");
    lCursorAdapter.newView(this, lSTime, getListView());
    lCursorAdapter.bindView(getListView(), this, lSTime);
    setListAdapter(lCursorAdapter);

Is ArrayAdapter the answer? If yes, could you share what parameters would you pass to it?

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

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

发布评论

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

评论(3

多彩岁月 2024-10-25 03:48:40

ArrayAdapter 是答案吗?

不,你不能在这里使用ArrayAdapter。由于您需要显示数据库中的数据,因此您必须在此处使用自定义 CursorAdapter

Is ArrayAdapter the answer?

No you can not use the ArrayAdapter here. Because you need to display data from the database therefore you must use here a Custom CursorAdapter.

悲歌长辞 2024-10-25 03:48:40

您可以向数据库中的投影添加一个附加字段。

这是我用来根据商品价格(以美分存储)和关联单位来格式化成本的投影示例。

public static final String CONCATE_COST
    = "'

将其用作列,您可以使用 columnR.id.* 的法线映射将其与视图中的字段配对。

|| CASE WHEN SUBSTR(ROUND("+COLUMN_PRICE +"/100.0, 2), LENGTH(ROUND("+COLUMN_PRICE +"/100.0, 2))-1, 1)='.' THEN ROUND("+COLUMN_PRICE +"/100.0, 2) || '0' else ROUND("+COLUMN_PRICE +"/100.0, 2) end || "+COLUMN_UNIT;

将其用作列,您可以使用 columnR.id.* 的法线映射将其与视图中的字段配对。

You could add an additional field to the projection from the database.

Here is an example of a projection I have used to format a cost based off of the a price of an item (stored in cents) and an associated unit.

public static final String CONCATE_COST
    = "'

Use that as a column and you can pair it with a field in your view using the normal mapping of column to R.id.*.

|| CASE WHEN SUBSTR(ROUND("+COLUMN_PRICE +"/100.0, 2), LENGTH(ROUND("+COLUMN_PRICE +"/100.0, 2))-1, 1)='.' THEN ROUND("+COLUMN_PRICE +"/100.0, 2) || '0' else ROUND("+COLUMN_PRICE +"/100.0, 2) end || "+COLUMN_UNIT;

Use that as a column and you can pair it with a field in your view using the normal mapping of column to R.id.*.

旧时光的容颜 2024-10-25 03:48:39

我建议使用定制适配器

I recommend to go for custom adapter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文