0

我正在尝试使用 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;



}

}

4

2 回答 2

0

尝试在适配器的 imageviewHolder 上执行此操作:

Connect.loadBitmap( 0, "http://www.saudeanimal.com.br/imagens/suricate06.jpg",
                new BitmapLoadListener() {
                    @Override
                    public void onLoadListener( int id, Bitmap bitmap ) {
                        if( bitmap != null ) {
                            tableBean.setImg( bitmap );
                            notifyDataSetChanged();
                        }
                    }
                } );
于 2013-04-20T15:11:58.153 回答
0

您的 MainActivity.java 应该是

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;
    private ArrayList list;

    public LoadImages(ArrayList list, Adapter adapter)
    {
        this.list = list;
        this.adapter = adapter;
    }

    @Override
    protected void onPostExecute(Elements divs)
    {
        super.onPostExecute(result);
        for ( org.jsoup.nodes.Element div : divs )
        {
            list.add(div);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        Document doc = null;
        try
        {
            doc = (Document) Jsoup.connect("http://mongol.co.il/").get();
            return  ((Elements) doc).select("img[src$=.jpg]");
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }
        return null;
    }
}

注意:该行中有 ClassCastException return ((Elements) doc).select("img[src$=.jpg]");。解决方案仅是从后台线程更新列表视图,而不是针对使用 logcat 错误编辑的问题(请参阅下面的评论以进行澄清)。

于 2013-04-20T15:14:53.127 回答