ListView里面嵌套spinner和editext

public class Adapter extends BaseAdapter {
        private ArrayList lines;
        private List data_list;
        private String[] checkListName;
        private List list;
        private Worker worker;
        // 存储以名值对。存放Spinner的Prompt和用户选中的值
        private Map allValues;

        public Adapter(ArrayList mLines, Worker worker,List list) {
            this.list=list;
            this.lines = mLines;
            this.worker = worker;
            data_list = new ArrayList();

            for (int i = 0; i < worker.getData().size() + 1; i++) {
                if (i == 0) {
                    data_list.add("");
                } else {
                    data_list.add(worker.getData().get(i-1).getRealName());
                }
            }
            checkListName = new String[worker.getData().size()];
            for (int j = 0; j < worker.getData().size(); j++) {
                aCache.put("工人" + j, "0");
                checkListName[j] = "工人" + j;
            }
            allValues = new HashMap();
            putAllValues();
        }

        private void putAllValues() {
            for (String str : checkListName) {
                allValues.put(str, 0);
            }
        }

        public void setAllValues(Map allValues) {
            this.allValues = allValues;
        }

        @Override
        public int getCount() {
            if (list.size()==0){
                return 1;
            }else {
                return list.size();
            }
        }

        @Override
        public Object getItem(int position) {
            return lines.get(position);
        }
        @Override
        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(SelectWorkerActivity.this).inflate(R.layout.listview_item, parent, false);
                holder.textView = (TextView) convertView.findViewById(R.id.editText1);
                holder.etLine = (EditText) convertView.findViewById(R.id.editText1);
                holder.spinner = (Spinner) convertView.findViewById(R.id.sp_test);


                ArrayAdapter adapter1 = new ArrayAdapter(SelectWorkerActivity.this, android.R.layout.simple_spinner_item, data_list);
                holder.spinner.setAdapter(adapter1);
                holder.spinner.setTag(position);
                holder.spinner.setOnItemSelectedListener(new ItemClickSelectListener(
                        holder.spinner));
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final Line line = lines.get(position);
            if (holder.etLine.getTag() instanceof TextWatcher) {
                holder.etLine.removeTextChangedListener((TextWatcher) (holder.etLine.getTag()));
            }
            if (TextUtils.isEmpty(line.getText())) {
                holder.etLine.setText("");
            } else {
                holder.etLine.setText(line.getText());
            }
            if (line.isFocus()) {
                if (!holder.etLine.isFocused()) {
                    holder.etLine.requestFocus();
                }
                CharSequence text = line.getText();
                holder.etLine.setSelection(TextUtils.isEmpty(text) ? 0 : text.length());
            } else {
                if (holder.etLine.isFocused()) {
                    holder.etLine.clearFocus();
                }
            }

            holder.etLine.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(final View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        final boolean focus = line.isFocus();
                        check(position);
                        if (!focus && !holder.etLine.isFocused()) {
                            holder.etLine.requestFocus();
                            holder.etLine.onWindowFocusChanged(true);
                        }
                    }
                    return false;
                }
            });

            final TextWatcher watcher = new SimpeTextWather() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (TextUtils.isEmpty(s)) {
                        line.setText(null);
                    } else {
                        line.setText(String.valueOf(s));
                    }
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    super.onTextChanged(s, start, before, count);
                    if (TextUtils.isEmpty(s)) {
                        aCache.put("数量" + position, "0");
                    } else {
                        aCache.put("数量" + position + "", s.toString());
                    }
                }
            };
            holder.etLine.addTextChangedListener(watcher);
            holder.etLine.setTag(watcher);
            String checkedName = checkListName[position];
            //关键代码,配合下面的相应事件使用。
            holder.spinner.setPrompt(checkedName);

            int spinnerOptionPosition = allValues.get(checkedName);
            holder.spinner.setSelection(spinnerOptionPosition);
            return convertView;
        }

        private void check(int position) {
            for (Line l : lines) {
                l.setFocus(false);
            }
            lines.get(position).setFocus(true);
        }

        class ViewHolder {
            TextView textView;
            EditText etLine;
            private Spinner spinner;
        }

        private class ItemClickSelectListener implements AdapterView.OnItemSelectedListener {
            Spinner spinner;

            public ItemClickSelectListener(Spinner spinner) {
                this.spinner = spinner;
            }

            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                if (TextUtils.isEmpty(data_list.get(position))) {
                    aCache.put(spinner.getPrompt().toString(), "0");
                    if (Integer.parseInt(spinner.getTag().toString()) parent) {

            }
        }

        //返回用于选中的所有值
        public Map getSelectValues() {
            return allValues;
        }
    }

    private ArrayList createLines(Worker worker) {
        ArrayList lines = new ArrayList<>();
        for (int i = 0; i < worker.getData().size(); i++) {
            aCache.put("数量" + i, "0");
            Line line = new Line();
            line.setNum(i);
            lines.add(line);
        }
        return lines;
    }

你可能感兴趣的:(安卓,安卓)