我有一个包含一堆微调器的 ListView ,但是当我滚动 ListView 时,微调器的值被重置。有没有办法来解决这个问题?这就是我的 ArrayAdapter 类的样子。
公共(public)类 ProductArrayAdapter 扩展 ArrayAdapter { private final 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 );
}
希望这对以后遇到同样问题的人有帮助
关于Android - 滚动 ListView 时微调器选择消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15072456/