0

我正在尝试使用图像转换 HTML 文本Html.fromHtml并将其设置在 ListView 中,但图像显示为正方形!我已经实现了一个 imageGetter 并且我设置了

StrictMode.ThreadPolicy 策略 = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);

避免android.os.NetworkOnMainThreadException

这是我的代码:

private class MyTask extends AsyncTask<Void, Void, Void>{

@Override
protected Void doInBackground(Void... arg0) {
try {
URL rssUrl = new URL(link);
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
                SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
                XMLReader myXMLReader = mySAXParser.getXMLReader();
                RSSHandler myRSSHandler = new RSSHandler();
                myXMLReader.setContentHandler(myRSSHandler);
                InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);

myRssFeed = myRSSHandler.getFeed(); 
            } catch (MalformedURLException e) {
                e.printStackTrace();    
            } catch (ParserConfigurationException e) {
                e.printStackTrace();    
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();    
            }

            return null;
        }

@Override
protected void onPostExecute(Void result) {
            if (myRssFeed!=null)
            {

                List<Map<String, String>> data = new ArrayList<Map<String, String>>();
                for (RSSItem item : myRssFeed.getList()) {
                    Spanned span = Html.fromHtml(item.getDescription().toString(), getImageHTML(), null);           
                    Map<String, String> datum = new HashMap<String, String>(0);
                    datum.put("title", item.getTitle());
                    datum.put("desc", span.toString());
                    data.add(datum);

                }
                SimpleAdapter adapter2 = new SimpleAdapter(getApplicationContext(), data, R.layout.listview, new String[] {"title", "desc"}, new int[] {R.id.text1,
                    R.id.text2});


                setListAdapter(adapter2);

这是 ImageGetter:

public ImageGetter getImageHTML(){
        ImageGetter ig = new ImageGetter(){
            public Drawable getDrawable(String source) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
             try{
              Drawable d = Drawable.createFromStream(new URL(source).openStream(), "src name");
              d.setBounds(0, 0, d.getIntrinsicWidth(),d.getIntrinsicHeight());
              return d;
             }catch(IOException e){
              Log.v("IOException",e.getMessage());
              return null;
             }
            }
           };
       return ig;
   }

有人能帮我吗?

4

1 回答 1

0

我不确定 myRssFeed.getList() 或 item.getDescription() 是否执行任何网络操作(只是猜测),只需尝试在 doInBackground 方法中添加您的 for 循环逻辑。

 List<Map<String, String>> data = new ArrayList<Map<String, String>>();
            for (RSSItem item : myRssFeed.getList()) {
                Spanned span = Html.fromHtml(item.getDescription().toString(), getImageHTML(), null);           
                Map<String, String> datum = new HashMap<String, String>(0);
                datum.put("title", item.getTitle());
                datum.put("desc", span.toString());
                data.add(datum);

            }
            adapter2 = new SimpleAdapter(getApplicationContext(), data, R.layout.listview, new String[] {"title", "desc"}, new int[] {R.id.text1,
                R.id.text2});

在 MyTask 中将 SimpleAdapter 适配器 2 声明为全局变量并将上面的代码片段移动到 doInBackground 方法

于 2013-03-11T03:53:27.497 回答