【问题标题】:update image in android custom adapter在android自定义适配器中更新图像
【发布时间】:2012-12-03 10:08:05
【问题描述】:

我需要更新自定义适配器中的第三个图像,我尝试使用 getView 方法但没有成功

RelativeLayout rl = (RelativeLayout) rowView.getParent();
ImageView image = (ImageView) rowView.findViewById(R.id.photo);
myCustomAdapter.getView(2, image, rl);              
image.setImageBitmap(bitmap);
myCustomAdapter.notifyDataSetChanged();

感谢任何帮助。

【问题讨论】:

  • notifyDataSetChanged 将重绘孔列表项,并为您的适配器调用 getView。我认为更新必须从您的适配器转发到 getView。
  • 那我需要改变什么?

标签: android android-listview android-arrayadapter custom-adapter


【解决方案1】:

getView() 是一个abstract 类(即在Adapter 类中没有实现,您需要实现它来更改从适配器返回的View 以填充View)。您应该在自定义适配器中实现它并检查位置值以更改图像。

基本上,每次您的Adapter 从数据中获取一个项目,然后将其传递给getView 以使用此数据填充将在此行中使用的View,都会发生这种情况。

【讨论】:

    【解决方案2】:
    //ListView list
    View view = list.getChildAt(position);
    
    ((ImageView)view.findViewById(R.id.list_image)).setImageBitmap(bitmap);
    

    R.id.list_image 是您的图像在 xml 中定义的自定义列表行中的 id。

    虽然这是一个临时解决方案,因为当列表滚动时,此项目稍后将由您的自定义适配器重新绘制。为此,您需要更改提供给自定义适配器的数组以设置项目。为此,您可能需要 Uri。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      相关资源
      最近更新 更多