如何改进ListView的外观?



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

我在做什么-我使用AsyncTask在后台运行下载XML内容的过程,并通过调用publishProgress(…)方法为每个列表项从doInBackground(…)方法填充ListView。当doInBackground(…)完成时,我已经收集了我想要显示的每个图像的URL,然后从onPostExecute(…)方法开始图像的下载过程并为每个下载的图像更新UI。对我来说,问题是ListView应该在开始下载可绘制文件和更新列表之前填充至少几个项目。当前ListView显示一个单独的项目,当填充下一个,所有的图像已经下载。我调试应用程序,我看到一个我停在onPostExecute(…)方法,并希望采取ListView项目的数量,它只返回一个,因为它显示。

是否有一种方法来强制外观Listview的项目,在开始下载之前,这是因为我有I-net连接,是足够的,没有延迟下载图像。

我只忘了提一下,每个图像都不大于10KB。

这是我的自定义AsyncTask:
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();
        }
    }
下面是自定义的Listadapter:
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;
        }
    }

…和Item类:

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

@MODIFICATIONS:

私有类DownloadFilesTask扩展AsyncTask> {onPreExecute() {

super.onPreExecute ();
        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();
    }
}

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

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

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

试试这个:

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

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

相关内容

  • 没有找到相关文章

最新更新