我到处寻找这个和所有可能的来源,但似乎都没有。我发现很多教程显示从ListView中的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>
,但效果不佳。
此外,我想通过片段实现此功能,因为此列表应该是侧面导航抽屉的一部分。
答案 0 :(得分:0)
如果在String中获取图像,则首先将该imageSting转换为位图。尝试以下代码
public static Bitmap StringToBitMap(String encodedString) {
try {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
现在您可以使用HashMap<String,Object>
答案 1 :(得分:0)
repo1.maven.org/maven2/com/squareup/picasso/picasso/2.3.4/picasso-2.3.4.jar下载此jar文件并将其添加到项目的libs
文件夹中。
在ImageLoaderAdapter
FindPeopleFragment
class ImageLoaderAdapter extends BaseAdapter {
private List<HashMap<String, String>> dataList;
public ImageLoaderAdapter(List<HashMap<String, String>> list) {
this.dataList = list;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
HashMap<String, String> map = dataList.get(position);
((TextView) convertView.findViewById(R.id.title)).setText(map.get("txt"));
((TextView) convertView.findViewById(R.id.date)).setText(map.get("cur"));
Picasso.with(getActivity()).load(map.get("flag"))
.into(((ImageView) convertView.findViewById(R.id.thumb)));
return convertView;
}
}
更新Description's
onPostExecute
,如下所示,
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);
*/
if (aList != null) {
ImageLoaderAdapter adapter = new ImageLoaderAdapter(aList);
setListAdapter(adapter);
}else{
Toast.makeText(getActivity(), "Data list being empty",
Toast.LENGTH_LONG).show();
}
}
使用for loop
更改doInBackground
,如下所示
for(Element l : links)
{
Elements img = l.select("img[src]");//This line changed
String imgSrc = img.attr("src");//This line changed
// 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++;
}
了解有关库check it
的更多信息