java - 如何在 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;
}
例如,我想把这张图片放在段落的这一部分。
解决方案
推荐阅读
- reactjs - 如何在 React 中找到当前引用
- java - 想要从 Windows Command Skipt(CMD) 文件中读取代码并提取信息
- reactjs - React SWR,如何过滤/排序数据
- python - 芹菜花中的活性柱代表什么
- amazon-ec2 - 使用 eksctl 创建 EKS 集群时 EC2 的 IAM 角色
- arrays - 在循环中创建数组和对象 - C
- javascript - 如何在使用 Jquery 输入搜索栏时添加显示/隐藏功能列表项?
- vue.js - v-img: require('../img/book.png') 是一个模块,无法加载
- flutter - 当 url 值更改时,Flutter Image.Network 不会重建/触发 api 调用
- node.js - MongoError:无法识别的管道阶段名称:'$push