2013-02-25 72 views
1

我有一個列表視圖,其中包含一堆的旋轉器,但是當我滾動列表視圖時,微調器的值被重置。有沒有辦法來解決這個問題?這就是我的ArrayAdapter類的樣子。Android的 - 微調器選擇消失當滾動ListView

public class ProductArrayAdapter extends ArrayAdapter private final Context context;

private final List<Product> values; 

public ProductArrayAdapter(Context context, List<Product> values) { 
    super(context, R.layout.product_item, values); 
    this.context = context; 
    this.values = values; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.product_item, parent, false); 
    Product availableProduct = values.get(position); 

    ((TextView) rowView.findViewById(R.id.productCode)).setText(Product.getProductName(availableProduct.getProductCode())); 

    ((Spinner) rowView.findViewById(R.id.count)).setAdapter(productCounts); 

    return rowView; 
} 

}

+0

每當你滾動時,你都在膨脹每一個列表項目,所以你失去了所有的舊值。您需要每次都在getView中重置它。 – 2013-02-25 17:12:14

+0

ListViews(和其他AdapterViews)會重新使用每行中的佈局,因此您需要將值保存在其他位置。請觀看[Turbo-Charge Your UI](http://www.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html)或[World of ListView](http://www.google.com/events/) /io/2010/sessions/world-of-listview-android.html)由Android的首席開發人員詳細介紹了這一主題。 – Sam 2013-02-25 17:13:53

回答

4

我能得到它的工作,但因爲沒有人回答我的問題,我只是發佈自己的解決方案。

我創建了一個名爲selectedItems的HashMap<Integer,Integer>來存儲ArrayAdapter的值,並將它們設置在onItemSelected偵聽器中。並根據HashMap設置Spinner當前選定的項目。

 if (selectedItems.get(position) != null) { 
    ((Spinner) rowView.findViewById(R.id.count)).setSelection(selectedItems.get(position)); 
    } 

    ((Spinner) rowView.findViewById(R.id.count)).setOnItemSelectedListener(new OnItemSelectedListener() { 

    public void onItemSelected(AdapterView<?> parent, View view, 
     int pos, long id) { 
     selectedItems.put(position, pos); 
    } 

希望這有助於在未來

+0

那麼你在哪裏設置「基於HashMap的Spinner當前選定的項目」? – 2016-01-25 06:50:28

0

萬一它有助於任何人,我是有一個ListView是有一個editText和一個spinner同樣的問題,其他人同樣的問題,並我對這兩個觀點都有問題。只要我通過使用onFocusChangeListener而不是onTextChangeListenersee solution here - 參見defpillz中的一個)解決了editText的問題,那麼微調問題就消失了。