2013-02-28 100 views
1

里面在我的ListFragment我有这样的:更新一个列表视图片段

private SelectedItemListAdapter selectedItemListAdapter; 
public void initSelectedItemListAdapter(CellItem[] itemList) 
{ 
    selectedItemListAdapter = new SelectedItemListAdapter(getSherlockActivity(), R.layout.listview_item_selecteditem, itemList); 
    setListAdapter(selectedItemListAdapter); 
} 

调用此方法可以让我在我的ListView但一切设定的数据我试图更新这个数据到目前为止失败。我设法实现它的唯一方法是创建一个SelectedItemListAdapter的新实例,我认为它不是非常有效。

的尝试我想是使用:

public void updateSelectedItemListAdapter(CellItem[] newList) 
{ 
    selectedItemListAdapter.clear(); 

    for(int i = 0; i < newList.length; i++) 
    selectedItemListAdapter.add(newList[i]); 

    setListAdapter(selectedItemListAdapter); 
    selectedItemListAdapter.notifyDataSetChanged(); 
} 

但是这给了我一个java.lang.UnsupportedOperationException。我也读过关于在主线程上运行这个,但它给了我同样的例外。

我也注意到,如果newList有不同的计数,我得到java.util.ArrayList.throwIndexOutOfBoundsException,这表明我错过了一些东西来刷新数据源。

按照要求,这里是我的SelectedItemListAdapter

public class SelectedItemListAdapter extends ArrayAdapter<CellItem> 
{ 
    Context context; 
    int layoutResourceId; 
    CellItem data[] = null; 
    private static final int ROW_ITEM = 0; 
    private static final int ROW_VIEWTYPE_COUNT = 1; 

    class CellItemHolder 
    { 
    LinearLayout rootLayout; 
    TextView itemName; 
    TextView itemValue; 
    } 

    public SelectedItemListAdapter(Context context, int layoutResourceId, CellItem[] data) 
    { 
    super(context, layoutResourceId, data); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
    View row = convertView; 
    CellItemHolder holder = null; 
    CellItem item = getItem(position); 

    if(row == null) 
    { 
     holder = new CellItemHolder(); 

     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
     row = inflater.inflate(layoutResourceId, parent, false); 

     holder.rootLayout = (LinearLayout)row.findViewById(R.id.itemlist_rootLayout); 
     holder.itemName = (TextView)row.findViewById(R.id.selectedItem_name); 
     holder.itemValue = (TextView)row.findViewById(R.id.selectedItem_value); 

     row.setClickable(true); 

     row.setTag(holder); 
    } 
    else 
    { 
     holder = (CellItemHolder)row.getTag(); 
    } 

    holder.itemName.setText(item.itemName); 
    holder.itemValue.setText(item.itemValue); 
    holder.rootLayout.setBackgroundColor(Color.WHITE); 

    return row; 
    } 

    @Override 
    public int getCount() 
    { 
    return data.length; 
    } 

    @Override 
    public int getViewTypeCount() 
    { 
    return ROW_VIEWTYPE_COUNT; 
    } 

    @Override 
    public int getItemViewType(int position) 
    { 
    return ROW_ITEM; 
    } 
} 

没有人有任何想法,我怎么可以更新我的列表适配器?

+0

不要犹豫,也为您的适配器添加代码。 – Luksprog 2013-02-28 15:44:10

+0

我刚刚添加了! – 2013-02-28 15:51:20

+0

SelectedItemListAdapter的add方法在哪里? – Luciano 2013-02-28 16:02:56

回答

2

但是,这给了我一个java.lang.UnsupportedOperationException。 I 也读了关于在主线程上运行这个,但它给我 相同的例外。

这与主UI线程无关,与适配器有关。问题是你使用一个适配器来传递一个数组数组。超类ArrayAdapter将采用该数据阵列并将其转换为List,对此,add()remove()方法未实现。没有这些方法意味着你的适配器在尝试clearadd元素(方法可用于开始使用API​​级别11,因此要小心)时会抛出UnsupportedOperationException(因为它使用列表的这些方法)。

的解决方案是使用一个正常ArrayList(或其它List)代替的CellItem阵列并传递到ArrayAdapter(其将能够修改它)。

我也注意到,如果newList有不同数量的 以前,我得到java.util.ArrayList.throwIndexOutOfBoundsException, 这表明我失去了一些东西来刷新数据源。

我不知道是什么原因造成的,你的代码应该在任何边界超越之前抛出UnsupportedOperationException异常。

+0

谢谢。我不知道为什么我使用数组而不是列表!:D – 2013-02-28 16:34:01

+0

@ ing0作为一个提示,如果你打算覆盖'ArrayAdapter'的大部分方法,那么你可以扩展'BaseAdapter'并避免其他可能的问题。 – Luksprog 2013-02-28 16:35:42

+0

噢好的谢谢,从来没有想过这样做。 – 2013-02-28 16:38:15