我正在尝试使用 AsyncTask、ListView 和 Jsoup 从网站检索照片到我的 android 应用程序中。在 2 个类之间存在一些困难(一个扩展 Activity,另一个扩展 AsyncTask)这是我的主要活动:
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();
}
这是 AsyncTask 类:
public class LoadImages extends AsyncTask {
private ArrayAdapter adapter;
私有 ArrayList 列表;
public LoadImages(ArrayList list, Adapter adapter)
{
this.list = list;
this.adapter = (ArrayAdapter) adapter;
}
protected void onPostExecute(Elements divs) {
// TODO Auto-generated method stub
super.onPostExecute(divs);
for (org.jsoup.nodes.Element div : divs)
{
list.add(div);
}
adapter.notifyDataSetChanged();
}
@Override
protected Object doInBackground(Object... params) {
Document doc = null;
try {
doc = (Document) Jsoup.connect("http://mongol.co.il/").get();
Elements divs =((Elements) doc).select("img[src$=.jpg]");
} catch (IOException e1) {
e1.printStackTrace(); }
return null;
}
}