我正在尝试使用小图像创建一个 ImageViews 列表,并通过单击它们来显示大图。我在 AsyncTask 中使用解析器从站点下载。
第一步(“doInBackground”)解析器从小图像中读取 url 并将它们放入 ArrayList<HashMap<String, String>>
. 我使用密钥“imgUrlSmall”
然后从大图像中读取 url 并放入同一个 ArrayList。使用键“imgUrlBig”
在“onPostExecute”中,它创建 ImageViews 并设置图像imageViewArray[i].setImageDrawable(grabImageFromUrl(text));
然后它设置了 ClickListener,在点击图片时它开始新的活动并将大图片的 url 放在那里。
现在程序显示所有图像(大小)。如何仅将带有键“imgUrlSmall”的值放入 imageViewArray。并 putExtra 具有正确的价值?
也许是一些验证,比如如果map.containsKey?
对于第二部分,为了使 putExtra 可以将所有带有键“imgUrlBig”的值设置为标准ArrayList<String>
,然后单击图像使用v.getId()
?
对不起我的英语不好
这里是所有代码:
做背景
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
ArrayList<HashMap<String, String>> imgItems = new ArrayList<HashMap<String, String>>();
/* --First Parser-- */
HtmlCleaner htmlCleaner = new HtmlCleaner();
URL url = new URL(BLOG_URL);
TagNode root = htmlCleaner.clean(url);
Object[] statsNode3 = root.evaluateXPath(XPATH_STATS3);
for (Object tag : statsNode3){
TagNode aTag = (TagNode) tag;
String href = aTag.getAttributeByName("src").trim();
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("imgUrlSmall", href);
// adding HashList to ArrayList
imgItems.add(map);
}
/* --Second Parser-- */
HtmlCleaner htmlCleaner2 = new HtmlCleaner();
URL url2 = new URL(BLOG_URL);
TagNode root2 = htmlCleaner2.clean(url2);
Object[] statsNode_Full_Image = root2.evaluateXPath(XPATH_STATS4);
for (Object tag : statsNode_Full_Image){
TagNode aTag = (TagNode) tag;
String href = aTag.getAttributeByName("href").trim();
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("imgUrlBig", href);
// adding HashList to ArrayList
imgItems.add(map);
}
return imgItems;
}
后执行
protected void onPostExecute( ArrayList<HashMap<String, String>> imgItems) {
String text="";
int i=0;
ImageView[] imageViewArray = new ImageView[imgItems.size()];
for (HashMap<String, String> map : imgItems)
for (Entry<String, String> mapEntry : map.entrySet())
{
text = mapEntry.getValue();
// create ImageView
imageViewArray[i] = new ImageView(Test4.this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50,50);
imageViewArray[i].setImageDrawable(grabImageFromUrl(text));
imageViewArray[i].setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v) {
System.out.println(v.getId());
Intent intent = new Intent(v.getContext(), Full_Image_Activity.class);
intent.putExtra("imgUrlBig", url); //Here i don`t know how to put the url
startActivityForResult(intent, 0);
}
});
// Let's get the root layout and add our ImageView
RelativeLayout layout = (RelativeLayout) findViewById(R.id.moreImages);
layout.addView(imageViewArray[i], params);
i++;
}
}