用户最近帮我用JSoup获取图像。我用Picasso创建了一个ViewPager并创建了一个Test String [],它正在工作。
现在我想把那些在JSoup中的图片拉进毕加索。
有人可以帮忙吗?
public class MainActivity extends AppCompatActivity {
String url = "http://www.readmangaonline.org/manga/one-piece/902/1";
private String[] imageUrls = new String[]{
"http://i999.mangapanda.com/one-piece/902/one-piece-10510603.jpg",
"http://i999.mangapanda.com/one-piece/902/one-piece-10510603.jpg"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ImageScrapAsyncTask().execute(url);
ViewPager viewPager = findViewById(R.id.viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
}
}
class ImageScrapAsyncTask extends AsyncTask<String, Void, Document> {
@Override
protected Document doInBackground(String... urls) {
try {
return Jsoup.connect(urls[0]).get();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Document document) {
if (document != null) {
Elements imgElements = document.select("img");
List<String> images = new ArrayList<>();
for (Element element : imgElements) {
String image = element.attr("data-src");
Log.d("IMAGE_URL", image);
images.add(image);
}
}
}
}
答案 0 :(得分:1)
在onPostExecute中设置适配器(在数据可用之后。)
@Override
protected void onPostExecute(Document document) {
if (document != null) {
Elements imgElements = document.select("img");
List<String> images = new ArrayList<>();
for (Element element : imgElements) {
String image = element.attr("data-src");
Log.d("IMAGE_URL", image);
if(image!=null && !image.equals("")){
images.add(image);
}
}
String[] imgArray = images.toArray(new String[images.size()]);
ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this, imgArray);
viewPager.setAdapter(adapter);
}
}
注意:使viewPager成为全局。