Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将图像从drawable添加到ListView从数据库填充_Android_Image_Listview_Drawable_Simplecursoradapter - Fatal编程技术网

Android 如何将图像从drawable添加到ListView从数据库填充

Android 如何将图像从drawable添加到ListView从数据库填充,android,image,listview,drawable,simplecursoradapter,Android,Image,Listview,Drawable,Simplecursoradapter,我想知道如何将drawables中的图像添加到我的listview中。我有两个文本视图(id、活动)的ListView,我从数据库中检索它们。你能给我一些建议吗?:) 我带着数据 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.history_list_item, cursor, new String[] { Tracks._ID,

我想知道如何将drawables中的图像添加到我的listview中。我有两个文本视图(id、活动)的ListView,我从数据库中检索它们。你能给我一些建议吗?:)

我带着数据

        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 });
我想在文本视图旁边显示图像,这取决于活动值


关于

您需要编写自己的适配器,该适配器扩展了SimpleCorsOrAdapter,然后重写getView方法,将textview的图像设置为coumpund drawable

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

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;
    }
私有类NotesListAdapter扩展了ArrayAdapter{
公共便笺stadapter(){
super(getBaseContext(),R.layout.list\u note\u行,R.id.noteHead,notes);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
if(convertView==null){
LayoutInflater充气机=getLayoutInflater();
convertView=充气机。充气(R.layout.list\u note\u row,parent,false);
holder=新的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_图标));
convertView.setTag(支架);
}
其他的
holder=(ViewHolder)convertView.getTag();
//设定文本
holder.txtHeading.setText(“bla-bla”);
holder.txtHeading.setCompoundDrawables(getResources().getDrawable(R.drawable.app_图标),null,null,null);
holder.txtContent.setText(“bla-bla”);
返回视图;
}
}
//这是Google IO为ListView建议的更好的方法
静态类视窗夹{
TextView-txtHeading;
文本视图TXT内容;
图像视图图像;
}

您需要编写自己的适配器,扩展SimpleCursorAdapter,然后重写getView方法,将textview的图像设置为coumpund drawable

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

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;
    }
私有类NotesListAdapter扩展了ArrayAdapter{
公共便笺stadapter(){
super(getBaseContext(),R.layout.list\u note\u行,R.id.noteHead,notes);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
if(convertView==null){
LayoutInflater充气机=getLayoutInflater();
convertView=充气机。充气(R.layout.list\u note\u row,parent,false);
holder=新的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_图标));
convertView.setTag(支架);
}
其他的
holder=(ViewHolder)convertView.getTag();
//设定文本
holder.txtHeading.setText(“bla-bla”);
holder.txtHeading.setCompoundDrawables(getResources().getDrawable(R.drawable.app_图标),null,null,null);
holder.txtContent.setText(“bla-bla”);
返回视图;
}
}
//这是Google IO为ListView建议的更好的方法
静态类视窗夹{
TextView-txtHeading;
文本视图TXT内容;
图像视图图像;
}

如果我正确理解了您的问题,一个解决方案是在布局中包含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编程开始。我在一个项目中做了类似的事情,效果很好。我不知道是否有更简单的方法


致以最诚挚的问候。

如果我正确理解了您的问题,一个解决方案是在布局中包含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;
    }
});
有一点