从ListView中的Spinner检索数据

时间:2017-08-15 08:36:38

标签: android listview android-spinner

我正在实现一个自定义列表视图,其中包含Spinner,我有TextView以及其他每个列表项。我希望为列表视图的每一行获取微调器的选定值。但我得到的只是Spinner的第一项。任何人都可以帮忙吗?

我查看了很多帖子,但所有答案都需要完全更改代码。我不想再把所有代码都写完了。

这是我的自定义适配器:

public class CustomAdapter extends ArrayAdapter<DataModel> {

    private ArrayList<DataModel> dataSet;
    Context mContext;

    private static class ViewHolder {
        TextView txtName;
        CheckBox checkBox;
        TextView price;
        Spinner spinner;
        DataModel data1;

    }

    public CustomAdapter(ArrayList<DataModel> data, Context context) {
        super(context, R.layout.row_item, data);
        this.dataSet = data;
        this.mContext = context;

    }
    @Override
    public int getCount() {
        return dataSet.size();
    }

    @Override
    public DataModel getItem(int position) {
        return dataSet.get(position);
    }


    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {

        final ViewHolder viewHolder;
        final View result;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
            viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
            viewHolder.price = (TextView) convertView.findViewById(R.id.priceid);
            viewHolder.spinner = (Spinner) convertView.findViewById(R.id.spinner);

            viewHolder.spinner.setFocusable(false);

            result=convertView;
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            result=convertView;
        }

        final DataModel item = getItem(position);


        viewHolder.txtName.setText(item.name);
        viewHolder.checkBox.setChecked(item.checked);
        viewHolder.price.setText(item.price.toString());
        viewHolder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                item.setPortion(viewHolder.spinner.getSelectedItem().toString());
            }

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

            }
        });



        return result;
    }
}

我的数据模型:

public class DataModel {

    public String name;
    public Double price;
    boolean checked;
    public String portion;

    public void setPortion(String portion) {
        this.portion = portion;
    }

    public String getPortion() {
        return portion;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public Double getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public DataModel(String name, Double price, boolean checked) {
        this.name = name;
        this.price = price;
        this.checked = checked;
    }

    public DataModel(String name) {
        this.name = name;
    }
}

0 个答案:

没有答案