首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何改善ListView的外观

如何改善ListView的外观
EN

Stack Overflow用户
提问于 2011-07-27 05:59:34
回答 2查看 530关注 0票数 0

目前,我面临以下问题:创建一个自定义的ListView (1 x ImageView + 2 x TextView)。它显示来自I-net的内容,这是一个XML文件。

我在做什么?我使用publishProgress(...)在后台运行下载doInBackground(...)内容并通过对doInBackground(...)方法中的每个列表项调用ListView来填充ListView的过程。当doInBackground(...)完成时,我已经收集了我想要显示的每个图像的URL,然后从onPostExecute(...)方法开始了图像的下载过程,并更新了每个下载的图像的UI。我的问题是,ListView应该在开始下载一个可绘制文件并更新列表之前至少填充几个项目。当前,ListView显示一个项目,当填充下一个条目时,所有图像都已经下载。我调试了这个应用程序,我发现我在onPostExecute(...)方法中停了下来,想要获取ListView条目的数量,它只返回一个,就像它显示的那样。

在开始下载之前,是否有一种方法可以强制显示Listview的项目,因为我所拥有的that连接已经足够了,而且下载图像不会延迟。

我忘了只提到,每一幅图像都不会比10 is大。

这是我的定制AsyncTask

代码语言:javascript
运行
AI代码解释
复制
private class DownloadFilesTask extends AsyncTask<String, Object, List<RSSItem>> {
        protected void onPreExecute() {
            super.onPreExecute();

            la.clear();
        }
        protected List<RSSItem> doInBackground(String... urls) {
            parse(urls[0]);

            if (feed == null) { return null; }

            rssList = feed.getAllItems();
            Iterator<RSSItem> iterator = rssList.iterator();

            while(iterator.hasNext()) {
                if (isCancelled()) return null; 
                RSSItem rss = iterator.next();
                publishProgress(rss);
            }

            return rssList;
        }
        protected void onProgressUpdate(Object... progress) {
            super.onProgressUpdate(progress);

            la.add((RSSItem)progress[0]);
            la.notifyDataSetChanged();
        }
        protected void onPostExecute(List<RSSItem> result) {
            super.onPostExecute(result);

            Drawable downloadedImage;
            for (int z = 0; z < rssList.size(); z++) {
                downloadedImage = downloadImage(rssList.get(z).getImageURL());
                ((RSSItem)rssList.get(z)).setImage(downloadedImage);
                la.notifyDataSetChanged();
            }
        }
        protected void onCancelled() {
            super.onCancelled();
        }
    }

以下是自定义的列表适配器

代码语言:javascript
运行
AI代码解释
复制
private class ListAdapter extends ArrayAdapter<RSSItem> {

        private List<RSSItem> items;

        public ListAdapter(Context context, int textViewResourceId, List<RSSItem> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item, null);
            }

            RSSItem item = items.get(position);

            if (item != null) {
                ImageView itemImage = (ImageView) v.findViewById(R.id.ivImage);
                TextView title = (TextView) v.findViewById(R.id.tvTitle);
                TextView description = (TextView) v.findViewById(R.id.tvDescription);
                if (item.getImage() != null) {
                    itemImage.setBackgroundDrawable(item.getImage());
                } else {
                    itemImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon));
                }
                if (title != null) {
                    title.setText(item.getTitle());
                }
                if (description != null) {
                    description.setText(item.getDescription());
                }
            }

            return v;
        }
    }

...and Item类:

代码语言:javascript
运行
AI代码解释
复制
public class RSSItem {
    private String data = null;
    private String description = null;
    private Drawable image = null;
    private String imageUrl = null;
    private String title = null;

    RSSItem() {
    }

    public void setData(String data) {
        this.data = data;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setImageURL(String imageURL) {
        this.imageUrl = imageURL;
    }

    public void setImage(Drawable image) {
        this.image = image;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getData() {
        return data;
    }

    public String getDescription() {
        return description;
    }

    public Drawable getImage() {
        return image;
    }

    public String getImageURL() {
        return imageUrl;
    }

    public String getTitle() {
        return title;
    }
}

@修改:

私有类DownloadFilesTask扩展了AsyncTask> {受保护的onPreExecute() { super.onPreExecute();

代码语言:javascript
运行
AI代码解释
复制
        la.clear();
    }

    protected List<RSSItem> doInBackground(String... urls) {
        parse(urls[0]);

        if (feed == null) { return null; }

        rssList = feed.getAllItems();
        publishProgress(true);

        Drawable downloadedImage;
        for (int z = 0; z < rssList.size(); z++) {
            downloadedImage = downloadImage(rssList.get(z).getImageURL());
            ((RSSItem)rssList.get(z)).setImage(downloadedImage);
        }
        return rssList;
    }

    protected void onProgressUpdate(Object... progress) {
        super.onProgressUpdate(progress);

        la.notifyDataSetChanged();
    }

    protected void onPostExecute(List<RSSItem> result) {
        super.onPostExecute(result);

        la.notifyDataSetChanged();
    }

    protected void onCancelled() {
        super.onCancelled();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-27 06:19:32

我不确定我是否理解你的问题,但我要做的是(如果我做对了):

我想您有一个存储在ListView中的自定义对象,比方说CustomItem。我会将下载的URL放在该对象的构造函数中,并将Drawable存储为CustomItem的成员。就在您创建了一个新的CustomItem并获得了它的映像之后,publishProgress()并在列表适配器上调用了notifyDataSetChanged()

编辑:您应该将代码从onPostExecute()方法的末尾移到doInBackground()方法的末尾。

试试这个:

代码语言:javascript
运行
AI代码解释
复制
private class DownloadFilesTask extends AsyncTask<String, RSSItem, Void> {
    protected void onPreExecute() {
        super.onPreExecute();
        la.clear();
    }
    protected List<RSSItem> doInBackground(String... urls) {
        parse(urls[0]);

        if (feed == null) { return null; }
        rssList = feed.getAllItems();
        Iterator<RSSItem> iterator = rssList.iterator();

        while(iterator.hasNext()) {
            if (isCancelled()) return null; 
            RSSItem rss = iterator.next();
            //this happens fast no need to do this
            publishProgress(rss);
            //la.add(rss);
        }
        Drawable downloadedImage;
        for (int z = 0; z < rssList.size(); z++) {
            downloadedImage = downloadImage(rssList.get(z).getImageURL());
            ((RSSItem)rssList.get(z)).setImage(downloadedImage);
            publishProgress(null);
        }
        return rssList;
    }
    protected void onProgressUpdate(RSSItem... progress) {
        super.onProgressUpdate(progress);
        if progress!=null
           la.add((RSSItem)progress[0]);
        la.notifyDataSetChanged();
    }
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

    }
    protected void onCancelled() {
        super.onCancelled();
    }
}
票数 2
EN

Stack Overflow用户

发布于 2011-07-27 06:15:38

您是否正在doInBackground()中填充列表适配器的底层数据集?然后,除了调用publishProgress()之外,还需要在onProgressUpdate() of onProgressUpdate() AsyncTask中调用adapter.notifyDataSetChanged()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6845662

复制
相关文章

相似问题

领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文