我将数据存储在SQLite数据库中。我存储的数据在 HTML 标签中,例如,“谁是好领导?”
实际上,我的任务是显示一个问题及其相关图像。我将图像保存在一个可绘制的文件夹中。现在,当问题与图像一起显示时,它也应该显示。我如何完成这项任务?
如果您确实受限于解析 html,则假设您在解析的内容中有唯一的非重复标记,则一系列 substring 和 indexOf 调用将起作用。
例如,您可以这样做:
//Set markers that will act as unique identifiers for when the image name begins/ends
final String beginningMarker = "src=\"";
final String endingMarker = "\"";
//Define what html we are parsing
String html = "<img alt=\"\" width=\"248\" height=\"170\" src=\"/test/image1.png\" />";
//Use our markers to find out the locations in the html where the image name begins/ends
int imgStringStartIndex = html.indexOf(beginningMarker) + beginningMarker.length();
int imgStringEndIndex = html.indexOf(endingMarker, imgStringStartIndex);
//Use the locations we just found to extract the image name
String imageName = html.substring(imgStringStartIndex, imgStringEndIndex);
System.out.println(imageName);