I have looked for this everywhere and all possible sources but none seems to work. I found many tutorials that showed fetching and displaying image from URL in ListView but they just don't work for fragments.
Here is my code which I'm using to fetch title and links from url but images just dont come
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);
}
}
}
I know that hashmap<string,string>
will not be able to display images but I have also tried hashmap<string,object>
but it doesn't work as well.
Also I want to implement this through fragments because this list should be a part of side navigation drawer.