在异步堆栈中加载位图时出现问题

我成功地解析了xml,并从标签和属性中获取了文本和URL。文本我已成功设置到我的列表视图中,但我无法将我的位图加载到图像视图中。我想我忘记了一些小事,比如某个地方的转换,或者我写错了AsyncTask。


这是我的解析和异步任务调用:


    private List<SomeItem> items = new ArrayList();


listView = findViewById(R.id.listView);

        IV = findViewById(R.id.iconsrc);


        ArrayList<String> list = new ArrayList<>();

        try {

            XmlPullParser parser = getResources().getXml(R.xml.myxml);


            while (parser.next() != XmlPullParser.END_DOCUMENT) {

                tagname = parser.getName();

                switch (parser.getEventType()){


                    case XmlPullParser.START_TAG:

                        if (tagname != null && tagname.equals(iconsrc)){

                            iconsrcVALUE = parser.getAttributeValue(0);

                            myBitmap = new AsyncForBitmap().execute(iconsrcVALUE).get();

                            IV.setImageBitmap(myBitmap);

                        }


                        break;


                    case XmlPullParser.TEXT :

                        tagtext = parser.getText();

                         break;


                    case XmlPullParser.END_TAG:


                        if (tagname != null && tagname.equals(displayname)) {


                            programmes.add(new SomeItem(tagtext, myBitmap));

                        }


                        break;

                }

            }


        } catch (Throwable t) {

            Toast.makeText(this,

                    "Error while loading xml document: " + t.toString(), Toast.LENGTH_LONG)

                    .show();

        }


        SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);

        listView.setAdapter(adapter);

也许我必须在“执行后”()方法上添加(覆盖)?但是我需要在那里做什么呢?



动漫人物
浏览 74回答 2
2回答

喵喵时光机

您可以执行一些操作,例如在构造函数中将图像视图传递到异步任务中。为此,首先修改您的异步任务,如下所示:class AsyncForBitmap extends AsyncTask<String, Void, Bitmap> {&nbsp; &nbsp; private final ImageView mImageView;&nbsp; &nbsp; public AsyncForBitmap(ImageView imageView){&nbsp; &nbsp; &nbsp; &nbsp; mImageView = imageView;&nbsp; &nbsp; }&nbsp; &nbsp; private Exception exception;&nbsp; &nbsp; protected Bitmap doInBackground(String... urls) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL url=new URL(urls[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap bitmap = BitmapFactory.decodeStream((InputStream)url.getContent());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return bitmap;&nbsp; &nbsp; &nbsp; &nbsp; } catch (MalformedURLException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; protected void onPostExecute(Bitmap bitmap){&nbsp; &nbsp; &nbsp; &nbsp; mImageView.setImageBitmap(bitmap);&nbsp; &nbsp; }}然后,只需执行以下操作:&nbsp;...&nbsp;case XmlPullParser.START_TAG:&nbsp; &nbsp; &nbsp;if (tagname != null && tagname.equals(iconsrc)){&nbsp; &nbsp; &nbsp; &nbsp; iconsrcVALUE = parser.getAttributeValue(0);&nbsp; &nbsp; &nbsp; &nbsp; new AsyncForBitmap(IV).execute(iconsrcVALUE);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;break;

慕运维8079593

试试这个代码protected void onPostExecute(Bitmap bitmap) {&nbsp; &nbsp; &nbsp; &nbsp; runOnUiThread(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mImageView.setImageBitmap(bitmap);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java