Android的 - 滚动ListView控件时微调选择在前看不见 [英] Android - Spinner selection disapears when scrolling ListView

查看:170
本文介绍了Android的 - 滚动ListView控件时微调选择在前看不见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含了一堆纺纱的列表视图,但是当我滚动列表视图,对于微调的值复位。有没有办法解决这一问题?这就是我的ArrayAdapter类的样子。

I have a listview that contains a bunch of spinners, but when I scroll the listview, the value for the spinner is reset. Is there a way to fix this? This is what my ArrayAdapter class looks like.

公共类ProductArrayAdapter扩展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;
}

}

推荐答案

我能得到它的工作,但因为没有人回答我的问题,我只是张贴自己的解决方案。

I was able to get it working, but since no one answered my question, I'll just post the solution myself.

我创建了一个的HashMap&LT;整数,整数GT; 名为selectedItems来存储一个ArrayAdapter的价值观,使他们在onItemSelected监听器。而基于HashMap的设置Spinner的当前选择的项目。

I created a HashMap<Integer,Integer> called selectedItems to store the values of the ArrayAdapter, and set them in the onItemSelected listener. And set the current selected item of the Spinner based on the HashMap.

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

希望这有助于其他人同样的问题在未来

Hopefully this is helpful to anyone else with the same problem in the future

这篇关于Android的 - 滚动ListView控件时微调选择在前看不见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆