Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.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 为ListView提供自定义类';转换视图_Android - Fatal编程技术网

Android 为ListView提供自定义类';转换视图

Android 为ListView提供自定义类';转换视图,android,Android,我的应用程序实现了一个列表视图。列表中的每个项目都显示一张照片和一些文本。这两个都是从互联网上下载的。我想为ListAdapter中指定的convertView提供我自己的自定义类,以便在下载映像时更新它。但是,我不知道如何提供视图的替代方案(我创建了一个扩展视图的子类,但是当我更改默认方法签名时) View getView(int position, View convertView, ViewGroup parent) 到 我有一个编译错误 如何为convertView提供自己的类 I w

我的应用程序实现了一个列表视图。列表中的每个项目都显示一张照片和一些文本。这两个都是从互联网上下载的。我想为ListAdapter中指定的convertView提供我自己的自定义类,以便在下载映像时更新它。但是,我不知道如何提供视图的替代方案(我创建了一个扩展视图的子类,但是当我更改默认方法签名时)

View getView(int position, View convertView, ViewGroup parent)

我有一个编译错误

如何为convertView提供自己的类

I would like to provide my own custom class for the convertView
如果您的
ListView
适配器需要两种或更多类型的视图,那么您需要在适配器和中重写。第一种方法返回您将拥有的视图类型,而第二种方法返回您应该拥有的当前项目位置的视图类型。类似于:

abstract class MyListAdapter implements ListAdapter {

    /**
     * Set this up in your constructor
     * */
    private Context context;
    private static final int VIEW_TYPE_SUPERMAN = 1;
    private static final int VIEW_TYPE_BATMAN = 2;
    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(position % 2 == 0) { // your business logic
            return VIEW_TYPE_BATMAN;
        }
        return VIEW_TYPE_SUPERMAN;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int viewType = getItemViewType(position);
        if(convertView == null) {
            if(viewType == VIEW_TYPE_BATMAN) {
                convertView = LayoutInflater.from(context).inflate(resource_id_for_batman, parent);
            } else {
                convertView = LayoutInflater.from(context).inflate(resource_id_for_superman, parent);
            }
            // setup a ViewHolder pattern implementation
        } else {
            // get your ViewHolder from convertView
        }
        // update your views from ViewHolder
        return convertView;
    }
}
进一步阅读:
,这样我可以在图像下载后更新它。
。因此,您不需要有单独的视图和类型。您只需要知道每个项目(基于其位置)如果:图像未下载、图像未能下载、下载正在进行或图像已下载。因此,我将通过添加此信息来实现
ViewHolder
模式。因此,您将有一个单一的视图类型,并且对于每种情况,您将决定要执行的操作(触发下载、从缓存/SD卡获取图像或显示默认图像)。为了更好地考虑这一点,我认为您需要在适配器实现中使用单独的结构,但在
ViewHolder
中不需要,因为
ViewHolder
对象将绑定到回收的视图

除非有
@Override
注释,否则上面的代码(用于添加重载的
视图getView(int-position,MyViewClass-convertView,ViewGroup-parent)
)应该编译。没有该注释,将不会调用重载方法

abstract class MyListAdapter implements ListAdapter {

    /**
     * Set this up in your constructor
     * */
    private Context context;
    private static final int VIEW_TYPE_SUPERMAN = 1;
    private static final int VIEW_TYPE_BATMAN = 2;
    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(position % 2 == 0) { // your business logic
            return VIEW_TYPE_BATMAN;
        }
        return VIEW_TYPE_SUPERMAN;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int viewType = getItemViewType(position);
        if(convertView == null) {
            if(viewType == VIEW_TYPE_BATMAN) {
                convertView = LayoutInflater.from(context).inflate(resource_id_for_batman, parent);
            } else {
                convertView = LayoutInflater.from(context).inflate(resource_id_for_superman, parent);
            }
            // setup a ViewHolder pattern implementation
        } else {
            // get your ViewHolder from convertView
        }
        // update your views from ViewHolder
        return convertView;
    }
}