获取Spinner的ListView中的位置并获取其选定的值

时间:2014-12-02 11:49:41

标签: android android-listview

我为ArrayAdapter实施了ListView

public class CalcAMRListAdapter extends ArrayAdapter<AMRStateListEntry> implements OnItemSelectedListener {

    public CalcAMRListAdapter(Context context, List<AMRStateListEntry> entries) {
        super(context, R.layout.activity_calc_amr_fragment_row, entries);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AMRStateListEntry entry = this.getItem(position);
        ViewHolder viewHolder = null;
        LayoutInflater inflater = null;

        if(convertView == null) {
            viewHolder = new ViewHolder();
            inflater = LayoutInflater.from(this.getContext());
            convertView = inflater.inflate(R.layout.activity_calc_amr_fragment_row, parent, false);
            viewHolder.spinnerActivity = (Spinner) convertView.findViewById(R.id.calc_amr_row_spinner_activity);
            viewHolder.spinnerHours = (Spinner) convertView.findViewById(R.id.calc_amr_row_spinner_hours);
            viewHolder.textViewPal = (TextView) convertView.findViewById(R.id.calc_amr_row_textview_pal);
            viewHolder.imageButtonDelete = (ImageButton) convertView.findViewById(R.id.calc_amr_row_button_delete);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String pal = null;

        switch (entry.getActivity()) {
        case 1: pal = "" + 0.95;
            break;
        case 2: pal = "" + 1.2;
            break;
        case 3: pal = "" + 1.5;
            break;
        case 4: pal = "" + 1.7;
            break;
        case 5: pal = "" + 1.9;
            break;
        case 6: pal = "" + 2.4;
            break;
        default: pal = "";
            break;
        }

        viewHolder.spinnerActivity.setSelection(entry.getActivity());
        viewHolder.spinnerActivity.setOnItemSelectedListener(this);
        viewHolder.spinnerHours.setSelection(entry.getHour());
        viewHolder.spinnerHours.setOnItemSelectedListener(this);
        viewHolder.textViewPal.setText(pal);
        viewHolder.imageButtonDelete.setImageResource(R.drawable.ic_remove_row);

        return convertView;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {}

    private static class ViewHolder {
        Spinner spinnerActivity = null;
        Spinner spinnerHours = null;
        TextView textViewPal = null;
        ImageButton imageButtonDelete = null;
    }
}

正如您所看到的,ListView的每个项目都包含两个Spinner。在onItemSelected方法中,我现在需要获取ListView中使用Spinner选择值的行的位置,并且我还需要在两个微调器之间有所区别行。我的数据模型是AMRStateListEntry。我需要在AMRStateListEntry内设置微调器的选定值。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

viewHolder.spinnerHours.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int positionInSpinner, long id) {
            // TODO Auto-generated method stub

        }

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

        }
    });

改为创建它。然后,您将public View getView(final int position, View convertView, ViewGroup parent)的位置设为final。现在,您可以在final int position内使用onItemSelected

为您的其他Spinner

做同样的事情