我有一个包含一堆微调器的列表视图,但是当我滚动列表视图时,微调器的值会被重置。有没有办法来解决这个问题?这就是我的 ArrayAdapter 类的样子。
公共类 ProductArrayAdapter 扩展 ArrayAdapter {
私有最终 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;
}
}
我能够让它工作,但由于没有人回答我的问题,我将自己发布解决方案。
我创建了一个HashMap<Integer,Integer>
调用 selectedItems 来存储 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 );
}
希望这对将来遇到同样问题的其他人有帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)