2013-02-28 137 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