我正在尝试创建一个包含小图像的ImageView列表,并通过单击它们来显示大图。我在AsyncTask中使用解析器从站点下载。
在第一步(“doInBackground”)解析器从小图像中读取URL并将它们放到ArrayList<HashMap<String, String>>
。我使用键“imgUrlSmall”
然后从大图像中读取URL并将其放到相同的ArrayList中。使用键“imgUrlBig”
在“onPostExecute”中,它会创建ImageView并设置图像imageViewArray[i].setImageDrawable(grabImageFromUrl(text));
然后设置ClickListener,在点击图片时启动新活动并将大图像的网址放在那里。
现在程序会显示所有图像(大小)。如何使用键“imgUrlSmall”将imageViewArray仅赋值。和putExtra的值是否正确?
可能是一些验证,例如map.containsKey?
对于第二部分,要使putExtra可以将所有带有键“imgUrlBig”的值放到标准ArrayList<String>
然后单击图像使用v.getId()
?
抱歉我的英文
这里是所有代码:
doInBackground
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;
}
onPostExecute
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++;
}
}
答案 0 :(得分:0)
您应该使用两个网址创建一个类,然后您可以使用ArrayList<HashMap<String, YourImageClass>>
类应该是这样的
public class YourImageClass{
public String smallImageUrl;
public String bigImageUrl;
public String someNameOfImageMaybe;
etc.
}