我正在尝试创建一个从网站上加载照片的简单应用程序(如9gag)。我正在使用jSoup和asyncTask。问题是,当我运行应用程序时,它只向我显示图像的URL。我该怎么做才能在listView中显示图像而不是url的图像。 这是我的LoadImages类:
public class LoadImages extends AsyncTask<String, Integer, Elements> {
private ArrayAdapter adapter;
private ArrayList list;
public LoadImages(ArrayList list, ArrayAdapter adapter)
{
this.list = list;
this.adapter = adapter;
}
@Override
protected Elements doInBackground(String... params) {
org.jsoup.nodes.Document doc = null;
try
{
doc =Jsoup.connect("http://mongol.co.il/").get();
return doc.select("img[src$=.png]");
}
catch (IOException e1)
{
e1.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Elements result) {
super.onPostExecute(result);
for ( org.jsoup.nodes.Element div : result )
{
list.add(div);
}
adapter.notifyDataSetChanged();
}
}
这是我的主要活动:
public class MainActivity extends Activity{
private ListView listView;
private ArrayAdapter adapter;
private ArrayList list;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
listView = (ListView) findViewById( R.id.listview);
list = new ArrayList();
adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
new LoadImages(list, adapter).execute();
}
}