try {
Document doc = Jsoup.parse(responseBody.string());
Elements contentsElements = doc.select("div#bo_v_con > p");
StringBuilder textContents = new StringBuilder();
for(Element element : contentsElements) {
if(element.select("img").size() > 0) {
if(textContents.length() > 0) {
Log.i(TAG, textContents.toString());
View textParent = getLayoutInflater().inflate(R.layout.article_text, articleBody, true);
TextView textView = textParent.findViewById(R.id.article_text_contents);
textView.setText(textContents.toString());
articleBody.addView(textView);
textContents = new StringBuilder();
Log.i(TAG, "textView is added");
}
Log.i(TAG, element.select("img").attr("src"));
View imageParent = getLayoutInflater().inflate(R.layout.article_image, articleBody, true);
ImageView imageView = imageParent.findViewById(R.id.article_image_contents);
GlideApp.with(ArticleActivity.this)
.load(element.select("img").attr("src"))
.override(Target.SIZE_ORIGINAL)
.into(imageView);
articleBody.addView(imageParent);
} else {
textContents.append(element.text().trim()).append("\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
这是我的代码。
如果元素具有<img>
标记,则在添加imageView之前使用StringBuilder的String添加textview。
运行Log.i(TAG, "textView is added")
articleBody.addView(textView)
没有消息,没有错误。刚停了。
删除膨胀代码时,会运行Log.i(TAG, "textView is added")
。
我错过了什么?