使用jSoup在应用程序中显示图像

时间:2013-04-24 17:37:03

标签: java android android-layout listview jsoup

我正在尝试创建一个从网站上加载照片的简单应用程序(如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();


    }

}

1 个答案:

答案 0 :(得分:0)

显示图片,而不是网址字符串。请查看此答案以获取更多信息:

Android: Image Reading from URL

Is there another way to display image from url?