我正在尝试创建一个从网站(如 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();
}
}