如何将可绘制图像添加到ListView并从数据库填充

发布于 2025-01-04 07:29:57 字数 399 浏览 1 评论 0原文

我想知道如何将可绘制对象中的图像添加到我的列表视图中。我有 ListView 和两个 TextViews(id, Activity),我从数据库检索。您能给我一些建议吗?:)

我正在获取数据,

        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 });

我想在取决于活动值的文本视图旁边显示图像。

问候

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?:)

I'm taking data with

        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 });

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

Regards

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

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

发布评论

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

评论(2

做个ˇ局外人 2025-01-11 07:29:57

您需要编写自己的适配器来扩展 SimpleCursorAdapter,然后重写 getView 方法以将文本视图的图像设置为可绘制的。

以下只是一个示例,但您可以按照自己的方式使用它:

private class NotesListAdapter extends ArrayAdapter<Note>{
        public NotesListAdapter() {
            super(getBaseContext(), R.layout.list_note_row, R.id.noteHead, notes);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;

            if(convertView == null){
                LayoutInflater inflater = getLayoutInflater();
                convertView = inflater.inflate(R.layout.list_note_row, parent, false);

                holder = new ViewHolder();
                holder.txtHeading = ((TextView)convertView.findViewById(R.id.noteHead));
                holder.txtContent = ((TextView)convertView.findViewById(R.id.noteBody));
                holder.image = ((ImageView)convertView.findViewById(R.id.note_icon));

                convertView.setTag(holder);
            }
            else
                holder = (ViewHolder) convertView.getTag();

            //set texts
            holder.txtHeading.setText("bla bla");
            holder.txtHeading.setCompoundDrawables(getResources().getDrawable(R.drawable.app_icon), null, null, null);
            holder.txtContent.setText("bla bla");

            return convertView;
        }
}

//this is better approach as suggested by Google-IO for ListView
    static class ViewHolder{
        TextView txtHeading;
        TextView txtContent;
        ImageView image;
    }

you need to write your own adapter which extends SimpleCursorAdapter and then override getView method to set images for your textview as coumpund-drawable.

Following is just an example, but you can use it in your own way:

private class NotesListAdapter extends ArrayAdapter<Note>{
        public NotesListAdapter() {
            super(getBaseContext(), R.layout.list_note_row, R.id.noteHead, notes);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;

            if(convertView == null){
                LayoutInflater inflater = getLayoutInflater();
                convertView = inflater.inflate(R.layout.list_note_row, parent, false);

                holder = new ViewHolder();
                holder.txtHeading = ((TextView)convertView.findViewById(R.id.noteHead));
                holder.txtContent = ((TextView)convertView.findViewById(R.id.noteBody));
                holder.image = ((ImageView)convertView.findViewById(R.id.note_icon));

                convertView.setTag(holder);
            }
            else
                holder = (ViewHolder) convertView.getTag();

            //set texts
            holder.txtHeading.setText("bla bla");
            holder.txtHeading.setCompoundDrawables(getResources().getDrawable(R.drawable.app_icon), null, null, null);
            holder.txtContent.setText("bla bla");

            return convertView;
        }
}

//this is better approach as suggested by Google-IO for ListView
    static class ViewHolder{
        TextView txtHeading;
        TextView txtContent;
        ImageView image;
    }
零度℉ 2025-01-11 07:29:57

如果我正确理解你的问题,一种解决方案是在你的布局中包含一个 ImageView,如下所示:

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 });

在上面的代码中,Tracks.SYMBOL 是带有资源标识的表列,R.id.symbol 是你的 ImageView 的 id 。然后,您必须实现 SimpleCursorAdapter.ViewBinder 接口并将您的实现设置到光标适配器中。

在看示例之前,需要考虑一个问题:游标列 Tracks.SYMBOL 可以包含整数,其值等于 R.drawable 类中所需的资源字段。这并不方便,因为这些字段是自动生成的,因此我们无法控制它们的值。一种解决方案是存储带有类字段名称的字符串,并使用反射来获取字段值。

下面是一个将 ViewBinder 附加到先前定义的适配器的示例:

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 就会认为工作已完成,并且不再尝试绑定该字段。

我刚刚开始Android 编程。我在一个项目中做了类似的事情,效果很好。不知道有没有更简单的方法。

此致。

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 });

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.

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.

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;
    }
});

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.

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.

Best regards.

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