Android - 滚动ListView时,微调器选择消失

时间:2013-02-25 17:10:40

标签: android

我有一个包含一堆微调器的列表视图,但是当我滚动列表视图时,微调器的值被重置。有没有办法来解决这个问题?这就是我的ArrayAdapter类。

public class ProductArrayAdapter extends ArrayAdapter {     私有最终上下文上下文;

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;
}

}

2 个答案:

答案 0 :(得分:6)

我能够让它工作,但由于没有人回答我的问题,我将自己发布解决方案。

我创建了一个名为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 );
    }

希望这对将来遇到同样问题的其他人有帮助

答案 1 :(得分:0)

万一它可以帮助任何人,我遇到了一个有一个editText和一个spinner的listView的问题,我遇到了这两个问题的问题。我使用editText而不是onFocusChangeListenersee solution here - 查看defpillz中的问题来解决onTextChangeListener问题时,旋转器的问题就消失了。< / p>