如何从绘制添加图片到ListView控件从数据库填充 [英] How to add image from drawable to ListView populate from database

查看:176
本文介绍了如何从绘制添加图片到ListView控件从数据库填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何从可绘制图像添加到我的列表视图。我有两个TextViews(ID,活动),我从数据库中检索的ListView。你能给我一些建议:?)

I'm wondering how to add images from drawables to my listview. I have ListView with two TextViews(id, activity) that i retrieve from database. Could You give me some advice?:)

我与

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.history_list_item, cursor, new String[] { Tracks._ID,
                    Tracks.ACTIVITY }, new int[] { R.id.id, R.id.activity });

接下来我TextView的依赖于活动值显示图像。

I want to display image next to textview that depend on activity value.

问候

推荐答案

如果我正确理解你的问题,一个解决办法是在您的布局中的ImageView,这样的:

If I properly understood your question, one solution would be to include an ImageView in your layout, like this:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
        R.layout.history_list_item, cursor, new String[] { Tracks.SYMBOL,
                Tracks._ID, Tracks.ACTIVITY },
                new int[] { R.id.symbol, R.id.id, R.id.activity });

在上面的code,Tracks.SYMBOL与资源标识表列和R.id.symbol是你的ImageView的ID。然后,你就必须实现SimpleCursorAdapter.ViewBinder接口,并设置您的实现入游标适配器。

In the code above, Tracks.SYMBOL is the table column with the resource identification and R.id.symbol is the id of your ImageView. Then you would have to implement the SimpleCursorAdapter.ViewBinder interface and set your implementation into the cursor adapter.

在我们看到一个例子,有一个考虑到进行:光标柱Tracks.SYMBOL可以具有与等于在R.drawable类所需的资源字段的值的整数。因为这些领域是自动生成的,因此不能给我们对他们的价值观的控制,这是不方便。一个解决办法是存储与类的字段名称的String,并使用反射来获取字段值。

Before we see an example, there is one consideration to be made: The cursor column Tracks.SYMBOL could have integers with values which are equal to the desired resource fields in the R.drawable class. This is not convenient since those fields are generated automatically, thus not giving us control over their values. One solution would be to store a String with the class field name and use reflection to get the field values.

下面是一个ViewBinder的一例附着到previously定义适配器:

Here is an example of a ViewBinder being attached to the previously defined adapter:

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        boolean binded = false;
        if (view instanceof ImageView) {
            int resourceId R.drawable.default_img;
            String image = cursor.getString(columnIndex).trim();
            try {
                resourceId = R.drawable.class.getField(image).getInt(null);
            } catch (NoSuchFieldException e) {
            }
            ((ImageView) view).setImageResource(resourceId);
            binded = true;
        }
        return binded;
    }
});

与setViewValue方法的一个问题是,如果返回false,那么SimpleCursorAdapter将执行它照常方式的结合。但是,如果该方法返回true,SimpleCursorAdapter认为工作做好,不要尝试在字段绑定了。

One point with the setViewValue method is that if it returns false, then SimpleCursorAdapter will perform the binding in the way it normally does. But if that method returns true, SimpleCursorAdapter considers the job done and do not try to bind that field anymore.

我刚开始与Android编程。我不喜欢的东西,在一个项目,它工作得很好。我不知道是否有更简单的方法。

I'm just starting with Android programming. I did something like that in one project and it worked fine. I don't know if there is an easier way.

最好的问候。

这篇关于如何从绘制添加图片到ListView控件从数据库填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆