2012-04-20 79 views
1

我遇到了一个问题,那就是我的ListView自定义ArrayAdapter(其中包含一个图库控件和两个TextView作为子控件)未更新图库控件及其内容。自定义ArrayAdapter不更新子控件

要设置应用程序的背景,我创建了一个应用程序,允许两个或更多设备通过蓝牙连接到另一个设备,并将每个设备的照片传输到其连接的设备。现在,为了实现,每个设备都以可滚动列表视图表示,其中包含图库窗口小部件(所有设备的图像以水平方式滚动)和两个TextView窗口小部件,用于标识发件人。

为ListView布局如下:

<LinearLayout ...> 
    <Gallery .../> 
    <LinearLayout> 
     <TextView .../> 
     <TextView .../> 
    </LinearLayout> 
</LinearLayout> 

的图像发送和接收都没有问题,我已经他们很反复测试。

该问题似乎与自定义ArrayAdapter的notifyDataSetChanged()方法行为不如预期。当我在ListView(在我的主Activity)中创建一个新的ImageHolderClass对象时,我按如下操作:mImageHolderAdapter.add(new ImageHolderClass(this, "Me", mBtAdapter.getAddress(), mLocalImageList));。知道适配器的add()方法应该调用notifyDataSetChanged(),它按照预期为第一个创建和添加的对象工作。但是,当我添加另一个对象或将任何新图像添加到对象的imageList时,它们会创建存储在第一个对象中的图像的精确副本。

现在,有趣的是,每个ImageHolderClass中的两个TextView变量设法更新,但图库中的图像似乎保持完全相同。

我试图绕过在ImageHolderAdapter()add()方法(用addItem())方法,然后迫使notifyDataSetChanged()只有在其他图像被添加,这似乎显示其他的图像(但不显示所有的人),但是只有设备和其他设备没有连接时才能使用。

我的自定义ArrayAdapter如下:

public class ImageHolderAdapter extends ArrayAdapter<ImageHolderClass>{ 
private ArrayList<ImageHolderClass> objects; 

public ImageHolderAdapter(Context context, int layoutResourceId, ArrayList<ImageHolderClass> objects) { 
    super(context, layoutResourceId, objects); 
    this.objects = objects; 
} 

public static class ViewHolder { 
    public Gallery gallery; 
    public TextView deviceName; 
    public TextView deviceAddress; 
    public ArrayList imageList; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Assign the view we are converting to a local variable 
    View v = convertView; 
    ViewHolder holder; 

    // first check to see if the view is null. If it is, then we have to inflate it. 
    if (v == null) { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.imageholder, null); 
     holder = new ViewHolder(); 
     holder.gallery = (Gallery)v.findViewById(R.id.gallery); 
     holder.gallery.setAdapter(new ImageAdapter(getContext(), objects.get(position).imageList)); 
     holder.deviceName = (TextView)v.findViewById(R.id.deviceName); 
     holder.deviceAddress = (TextView)v.findViewById(R.id.deviceAddress); 
     v.setTag(holder); 
    } else { 
     holder = (ViewHolder)v.getTag(); 
    } 

    final ImageHolderClass imageHolderClass = objects.get(position); 

    if (imageHolderClass != null) { 
     holder.gallery = imageHolderClass.getGallery(); 
     holder.deviceName.setText(imageHolderClass.getDeviceName()); 
     holder.deviceAddress.setText(imageHolderClass.getDeviceAddress()); 
    } 

    return v; 
} 

public class ImageAdapter extends BaseAdapter { 
    int mGalleryItemBackground; 
    private Context mContext; 
    ArrayList list; 

    public ImageAdapter(Context c, ArrayList list) { 
     mContext = c; 
     this.list = list; 
     TypedArray attr = mContext.obtainStyledAttributes(R.styleable.TestbedActivity); 
     mGalleryItemBackground = attr.getResourceId(R.styleable.TestbedActivity_android_galleryItemBackground, 0); 
     attr.recycle(); 
    } 

    public int getCount() { 
     return list.size(); 
    } 

    public Object getItem(int position) { 
     return list.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView = new ImageView(mContext); 

     imageView.setImageBitmap((Bitmap)list.get(position)); 
     imageView.setLayoutParams(new Gallery.LayoutParams(180, 150)); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setBackgroundResource(mGalleryItemBackground); 

     return imageView; 
    } 
} 

}

和ImageHolderClass,其上面使用如下:

public class ImageHolderClass { 
public Gallery gallery; 
public String deviceName; 
public String deviceAddress; 
public ArrayList imageList; 

public ImageHolderClass() { 
    super(); 
} 

public ImageHolderClass(Context c, String deviceName, String deviceAddress, ArrayList imageList) { 
    super(); 
    this.gallery = new Gallery(c); 
    this.deviceName = deviceName; 
    this.deviceAddress = deviceAddress; 
    this.imageList = imageList; 
} 

public Gallery getGallery() { 
    return gallery; 
} 

public void setGallery(Gallery gallery) { 
    this.gallery = gallery; 
} 

public String getDeviceName() { 
    return deviceName; 
} 

public void setDeviceName(String deviceName) { 
    this.deviceName = deviceName; 
} 

public String getDeviceAddress() { 
    return deviceAddress; 
} 

public void setDeviceAddress(String deviceAddress) { 
    this.deviceAddress = deviceAddress; 
} 

public void setImageList(ArrayList list) { 
    this.imageList = list; 
} 

public ArrayList getImageList() { 
    return imageList; 
} 

}

所得图像(两个ListView对象都有相同的图像(但不应该))如下图: http://i.stack.imgur.com/koL2Q.jpg

我试着换出Gallery小部件定制HorizontalListView,但是这给了我完全相同的结果,所以我知道这是不是由于Gallery部件实现。

更新

我修改我的代码单独添加每个图像的ImageHolderAdapter。之前,我会创建一个ArrayList,将所有图像存储在其中,然后在ImageHolderAdapter中创建一个新对象。下面的方法显示了如何将图像添加“散货”

public void loadImages(String userName, String deviceAddress, ArrayList imageList) { 
    mImageHolderAdapterList.add(new ImageHolderClass(this, userName, deviceAddress, imageList)); 
    mImageHolderAdapter.notifyDataSetChanged(); 
} 

现在,我创建了ImageHolderClass对象我填充imageList之前。然后我从ImageHolderAdapter(通过使用mImageHolderAdapter.getItem(position))获取特定对象,从imageList中获取add()ArrayList。无论何时我做出任何修改,我都会致电mImageHolderAdapter.notifyDataSetChanged()

但是,如果我单独填充图像,则不会显示任何图像。我所得到的是一个黑色的屏幕,图像应该是。

回答

1

您应该在if语句之外设置图库的适配器。

if (v == null) { 
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    v = inflater.inflate(R.layout.imageholder, null); 
    holder = new ViewHolder(); 
    holder.gallery = (Gallery)v.findViewById(R.id.gallery);   
    holder.deviceName = (TextView)v.findViewById(R.id.deviceName); 
    holder.deviceAddress = (TextView)v.findViewById(R.id.deviceAddress); 
    v.setTag(holder); 
} else { 
    holder = (ViewHolder)v.getTag(); 
} 

holder.gallery.setAdapter(new ImageAdapter(getContext(), objects.get(position).imageList)); 
+0

,它仍然不能解决问题。 – 2012-04-24 15:06:13