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

标签 android

我有一个包含一堆微调器的 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/

相关文章:

java - 找不到方法 AdvertisingIdClient.getAdvertisingIdInfo

android - 在Android中设置 map 类型,经常出现问题

android - 对话框android中的图像缩放选项

java - 如何去除 Android SeekBar 中 Thumb 周围的气泡?

android - 更好的 draw9patch 版本

android - 图库选定位置动画

android ndk : are -fPIC and -pie mututally exclusive?

Android gradle 再次复制 LICENSE.txt 文件

android - 透明背景在 api 14-15 上显示为黑色

android - android studio 中奇怪的文件/库结构