2

多数のスピナーを含むリストビューがありますが、リストビューをスクロールすると、スピナーの値がリセットされます。これを修正する方法はありますか?これは私のArrayAdapterクラスがどのように見えるかです。

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

}

4

2 に答える 2

6

私はそれを機能させることができましたが、誰も私の質問に答えなかったので、自分で解決策を投稿します.

ArrayAdapter の値を格納するために selectedItems という名前を作成し、HashMap<Integer,Integer>それらを 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 );
    }

これが将来同じ問題を抱えている他の人に役立つことを願っています

于 2013-02-25T19:58:46.240 に答える