首页 > 解决方案 > 如何在 Asynctask 中创建 ImageView?

问题描述

我正在创建一个运行 asynctask 的 Web 应用程序。使用 JSoup,每次代码循环遍历 div 容器中的图像时,我希望我的代码创建一个 ImageView 对象,在该对象中我将在 OnPostExecute 方法中放置某些图像。

例如我有这个页面:https ://www.flagrantdelit.ca/lever-le-voile/

我能够从 div class="entry-content" 中获取所有文本,但是每次我的代码循环通过包含 Image 的 ap 标记时,我都想创建一个包含相同图像的 ImageView。我需要保持与网站相同的格式。

我尝试检查 ap 标记中的空文本,然后创建一个 ImageView,但它给了我这个错误:
ImageView (android.content.Context) in ImageView cannot be applied to (com.example.leflagrantdlit.ArticlesFocused.DownloadData)  

@Override
        protected void onPostExecute(String s)
        {
            super.onPostExecute(s);
            Log.d(TAG, "onPostExecute: Ended downloading AsyncTask");

            tvFocused = findViewById(R.id.tvFocused);
            tvFocused.setText(stringBuilder);


        }


        private String downloadHtml(String urlPath)
        {

            try {

                Document doc = Jsoup.connect(urlPath).get();
                String title = doc.getElementsByClass("entry-title").get(0).text();
                Elements pContent = doc.select("div.entry-content > p");
                Elements imgContent = doc.select("div.entry-content > p > img");


                for (Element textContent : pContent)
                {
                    if(textContent == null){
                       ImageView imageView = new ImageView(this); 
                    }
                    stringBuilder.append(textContent.text());
                    Log.d(TAG, "downloadHtml: " + textContent.text());
                    stringBuilder.append("\n");

                }


                //String title = doc.getElementsByClass("entry-title").get(0).text();
                Log.d(TAG, "downloadHtml: " + urlPath + title + stringBuilder);


                return null;

            } catch (MalformedURLException e) {
                Log.e(TAG, "downloadHTML: Invalid Url " + e.getMessage());
            } catch (IOException e) {
                Log.e(TAG, "downloadHTML: IO Exception reading data: " + e.getMessage());
            } catch (SecurityException e) {
                Log.e(TAG, "downloadHTML: Security Exception. Need permission? " + e.getMessage());
            }

            return null;
        }

例如,我想把这张图片放在段落的这一部分。

![img] https://i.imgur.com/nYa6uZB.png

标签: javaandroidasynchronousandroid-asynctask

解决方案


推荐阅读