Spinner选择不适用于Android

时间:2014-07-01 07:44:17

标签: android android-listview android-spinner

我有一个listView,其中n行包含textview1(用于文本),textview2(用于id)和微调器。我想在多行中选择微调器。这里的问题是,一旦我滚动listView,选择就消失了(Recycle listView)。

我将id存储在ArrayList(选择了哪些行微调器)和微调器的选定位置。在listView的onScroll方法中,我尝试使用spinner.setSelection(position);,但它不起作用。

我的示例图片enter image description here

我的源代码:

    level2.setOnScrollListener(new OnScrollListener() {
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
    }
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        for (int i = 0; i < level2.getChildCount(); i++) {
            RelativeLayout relative_layout = (RelativeLayout)level2.getChildAt(i);
            TextView textview_1 = (TextView)relative_layout.getChildAt(0);
            TextView textview_2 = (TextView)relative_layout.getChildAt(1);
            Spinner spinner = (Spinner)relative_layout.getChildAt(2);
            spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView <  ?  > arg0,
                    View arg1, int arg2, long arg3) {
                    //save my id and spinner selection here in a ArrayList
                    //Here the position is get value from arraylist
                    spinner.setSelection(position);
                }
                public void onNothingSelected(AdapterView <  ?  > arg0) {
                    // TODO Auto-generated method stub
                }
            });
        }
    }
  });

这不是完整的代码,它只是一块。请帮帮我。

2 个答案:

答案 0 :(得分:0)

如果你想设置所选的项目,那么你必须使用代码spinner.setSelection(arg2);,因为你没有初始化你的位置变量,它的默认值将是0,所以它总是会显示第1项地选择。

答案 1 :(得分:0)

我会这样解决。试试这个。这只是示例活动。

public class MainActivity extends Activity {
    SpinnerAdapter ratingAdapter;
    ListView listView;
    List<String> listString;
    ArrayAdapter<String> dataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listString = new ArrayList<String>(); 
        for(int listCount = 0; listCount < 20 ; listCount++){
            listString.add("ListCount"+listCount);
        }

        List<String> spinnerData = new ArrayList<String>();
        for (int i = 0; i < listString.size(); i++) {
            spinnerData.add("MyTest="+i);
        }

        dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, spinnerData);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        fillAdapter();
    }

    private void fillAdapter() {
        ratingAdapter = new SpinnerAdapter(getApplicationContext());
        listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(ratingAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class ViewHolder {
        Spinner spinner;
    }

    class SpinnerAdapter extends ArrayAdapter {
        Context context;
        HashMap<Integer,Integer> selectedItems  = new HashMap<Integer, Integer>();

        public SpinnerAdapter(Context context) {
            super(context, R.layout.item_list_spinner_adaoter, listString);
            this.context = context;
        }

           public int getCount() {
                return listString.size();
            }
            public Object getItem(int position) {
                 return listString.get(position);
            }
            public long getItemId(int position) {
                return position;
            }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_list_spinner_adaoter, null);
                holder.spinner = (Spinner) convertView.findViewById(R.id.spinner1);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }


            holder.spinner.setAdapter(dataAdapter);
             if ( selectedItems.get( position ) != null ) {
                 holder.spinner.setSelection( selectedItems.get( position ) );
             }
            holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    selectedItems.put( position, arg2 );
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });

            return convertView;
        }
    }
}

在activity_main.xml中:

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:stackFromBottom="false"
        android:transcriptMode="disabled" >
    </ListView>

</LinearLayout>

在item_list_spinner_adaoter.xml中:

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />