如何从另一个WebService Bean调用Android中baseAdapter中的getView方法?

发布于 2024-08-26 11:27:09 字数 2426 浏览 8 评论 0原文

我的代码中的适配器如下,我扩展了基本适配器:

@Override
 public View getView(final int position, View convertView, ViewGroup parent) {
  ViewHolder vHolder;

  // if (convertView == null) {
  vHolder = new ViewHolder();
  convertView = mInflater.inflate(R.layout.home_item, null);
  vHolder.albumIcon = (ImageView) convertView
    .findViewById(R.id.albumIcon);
    try {
   Bitmap icon = aws.getAlbumImg(itemInfolist.get(position)
     .getAlbumInfoCol().get(0).getAlbumID(), 0);
   if (icon != null) {
    vHolder.albumIcon.setImageBitmap(icon);
   } else {
    vHolder.albumIcon.setImageBitmap(BitmapFactory.decodeResource(
      context.getResources(), R.drawable.album));
   }
  } catch (Exception e) {
   vHolder.albumIcon.setImageBitmap(BitmapFactory.decodeResource(
     context.getResources(), R.drawable.album));
  }

      convertView.setTag(vHolder);
  return convertView;
 }

但是,我异步下载想象,

当调用时 Bitmap icon = aws.getAlbumImg(itemInfolist.get(position).getAlbumInfoCol().get(0).getAlbumID(), 0);

部分未下载的图片将使用默认图片,当这些图片下载到另一个Web Service Bean中后,我希望Web Service Bean发送消息来调用此适配器中的getView方法,以实现自动刷新功能。

但是如果我按如下方式更改Web Service Download Bean,则会导致异常

03-19 07:46:33.241: ERROR/AndroidRuntime(716): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that create视图层次结构可以触及其视图。

HomeAdapter mHomeAdapter;

 public AlbumWS(HomeAdapter homeAdapter) {
  mHomeAdapter = homeAdapter;
 }

And after download,

public boolean getAlbumImgWS(final ArrayList<Integer> albumIDs) {

  new Thread(new Runnable() {

   @Override
   public void run() {
    // TODO Auto-generated method stub
    AlbumInfoWS aiws = new AlbumInfoWS();
    for (int i = 0; i < albumIDs.size(); ++i) {
     if (ABSCENTALBUMIMGS.contains(albumIDs.get(i))) {
      continue;
     }
     if (FunctionUtil.isExist(albumIDs.get(i))) {
      continue;
     }
     String urlPath = aiws.getAlbumImage("en_US",
       Config.IMG_ATTIBUTETYPE, albumIDs.get(i));
     boolean ret = FunctionUtil.simpleDownload(Config.HOST
       + urlPath, "/data/data/com.greysh.amped/img/"
       + albumIDs.get(i) + ".jpg");
     if (!ret) {
      if (!ABSCENTALBUMIMGS.contains(albumIDs.get(i))) {
       ABSCENTALBUMIMGS.add(albumIDs.get(i));
      }
     }
     mHomeAdapter.notifyDataSetChanged();
    }
   }
  }).start();
  return true;
 }

The adapter in my code as follows, I extends the base adapter:

@Override
 public View getView(final int position, View convertView, ViewGroup parent) {
  ViewHolder vHolder;

  // if (convertView == null) {
  vHolder = new ViewHolder();
  convertView = mInflater.inflate(R.layout.home_item, null);
  vHolder.albumIcon = (ImageView) convertView
    .findViewById(R.id.albumIcon);
    try {
   Bitmap icon = aws.getAlbumImg(itemInfolist.get(position)
     .getAlbumInfoCol().get(0).getAlbumID(), 0);
   if (icon != null) {
    vHolder.albumIcon.setImageBitmap(icon);
   } else {
    vHolder.albumIcon.setImageBitmap(BitmapFactory.decodeResource(
      context.getResources(), R.drawable.album));
   }
  } catch (Exception e) {
   vHolder.albumIcon.setImageBitmap(BitmapFactory.decodeResource(
     context.getResources(), R.drawable.album));
  }

      convertView.setTag(vHolder);
  return convertView;
 }

However, I download the imagine asynchronously,

When invoke
Bitmap icon = aws.getAlbumImg(itemInfolist.get(position).getAlbumInfoCol().get(0).getAlbumID(), 0);

Some pictures which haven't downloaded will use the default image, after these picutures have downloaded in another Web Service Bean, I want the Web Service bean sends a message to invoke the getView method in this adapter in order to implement the auto refresh function.

But if I change the Web Service Download Bean as follows,it will cause the exception

03-19 07:46:33.241: ERROR/AndroidRuntime(716): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

HomeAdapter mHomeAdapter;

 public AlbumWS(HomeAdapter homeAdapter) {
  mHomeAdapter = homeAdapter;
 }

And after download,

public boolean getAlbumImgWS(final ArrayList<Integer> albumIDs) {

  new Thread(new Runnable() {

   @Override
   public void run() {
    // TODO Auto-generated method stub
    AlbumInfoWS aiws = new AlbumInfoWS();
    for (int i = 0; i < albumIDs.size(); ++i) {
     if (ABSCENTALBUMIMGS.contains(albumIDs.get(i))) {
      continue;
     }
     if (FunctionUtil.isExist(albumIDs.get(i))) {
      continue;
     }
     String urlPath = aiws.getAlbumImage("en_US",
       Config.IMG_ATTIBUTETYPE, albumIDs.get(i));
     boolean ret = FunctionUtil.simpleDownload(Config.HOST
       + urlPath, "/data/data/com.greysh.amped/img/"
       + albumIDs.get(i) + ".jpg");
     if (!ret) {
      if (!ABSCENTALBUMIMGS.contains(albumIDs.get(i))) {
       ABSCENTALBUMIMGS.add(albumIDs.get(i));
      }
     }
     mHomeAdapter.notifyDataSetChanged();
    }
   }
  }).start();
  return true;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-09-02 11:27:09
android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.

此异常意味着您正在尝试从另一个线程更新 UI - 在 Android 中这是不允许的。
您应该使用 Handler 将更改发布到主 UI 线程。
而不是

new Runnable() {
    public void run() {
    ...
    }
};

尝试

mHandler.post(new Runnable() {
                  public void run() {
                  ...
                  }
              };);
android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.

This exception means that you are trying to update the UI from another thread - in Android this is not permitted.
You should use a Handler to post the changes to the main UI thread.
Instead of

new Runnable() {
    public void run() {
    ...
    }
};

try

mHandler.post(new Runnable() {
                  public void run() {
                  ...
                  }
              };);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文