【发布时间】:2014-10-06 07:12:02
【问题描述】:
我到处寻找这个和所有可能的来源,但似乎没有一个有效。我发现很多教程都展示了从 ListView 中的 URL 获取和显示图像,但它们不适用于片段。
这是我用来从 url 获取标题和链接的代码,但图像只是不来
public class FindPeopleFragment extends ListFragment {
TextView txtdesc;
TextView txtdesc2;
TextView txtdesc3;
String flags=Integer.toString(R.drawable.logo);
String posts;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new Description().execute();
}
class Description extends AsyncTask<Void, Void, Void> {
String title;
String link;
Bitmap bitmap = null;
int i=0;
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
protected void onPreExecute() { }
protected Void doInBackground(Void... params) {
try {
Document document = Jsoup.connect("http://www.marineinsight.com/shipping-news").get();
Elements links = document.select("article a");
// posts = links.text();
for(Element l : links)
{
Elements img = document.select("article a img[src]");
String imgSrc = img.attr("src");
URL newurl = new URL(imgSrc);
title=l.attr("title");
if(title==""){ continue; }
link=l.attr("href");
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", title);
hm.put("cur",link);
hm.put("flag",imgSrc);
aList.add(hm);
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.thumb,R.id.title,R.id.date};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.list_item, from, to);
setListAdapter(adapter);
}
}
}
我知道hashmap<string,string> 将无法显示图像,但我也尝试过hashmap<string,object>,但效果不佳。
我也想通过片段来实现这个,因为这个列表应该是侧面导航抽屉的一部分。
【问题讨论】:
-
stackoverflow.com/questions/23985786/… 检查 Alan Deep 的回答,您只有图片网址。只有您可以在图像视图中设置后,才需要从该 url 获取图像。
-
看看毕加索图书馆,它会让你远离痛苦的世界。非常容易使用。
-
@SathishKumar 我已经尝试过那个 helpStack 示例。但主要问题是我想在片段中执行此操作,我得到的所有示例都是为了活动。 M无法弄清楚是什么原因。谢谢
-
解析响应后能否得到正确的结果?检查
aList已正确填充。如果你得到正确的解析数据意味着剩下的事情很简单 -
@SathishKumar 是的,除了列表视图中的图像外,一切都是正确的。不知道图像有什么问题。你能帮我修改上面的代码,以便我也可以检索图像.. 我对 android 开发完全陌生,并且在工作 2 周后能够到这里。
标签: android image listview url