如何从Expandable ListView中的EditText获取文本

时间:2017-02-21 11:30:42

标签: android android-edittext expandablelistview

我有可扩展的listview,其中包含edittext作为子项,在此下面的ExpandableListView下我有按钮,当用户点击按钮然后我想从EditText获取文本并存储在字符串中,我在getChildView方法中尝试了以下代码

MyAdapter

public class ExListAdapter extends BaseExpandableListAdapter {

        private Activity context;
        private Map<String, List<String>> collections;
        private ArrayList<ListDetails> listItem;
        private TextView txtOk;

        public ExListAdapter(Activity context, ArrayList<ListDetails> listItem,
                                   Map<String, List<String>> collections, TextView txtOk) {
            this.context = context;
            this.collections = collections;
            this.listItem = listItem;
            this.txtOk = txtOk;
        }

        public Object getChild(int groupPosition, int childPosition) {
            return 1;
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Nullable
        public View getChildView(final int groupPosition, final int childPosition,
                                 boolean isLastChild, @Nullable View convertView, ViewGroup parent) {

            LayoutInflater inflater = context.getLayoutInflater();

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_child, null);
            }

            etChild = (EditText) convertView.findViewById(R.id.etChild);
txtOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    etChild.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    System.out.println("Value : " + childPosition + etChild.getText().toString());
                    Toast.makeText(context, etChild.getText().toString() + "...", Toast.LENGTH_SHORT).show();
                }
            });

            etChild.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_UP:
                            v.getParent().requestDisallowInterceptTouchEvent(false);
                            break;
                    }
                    return false;
                }
            });
            etChild.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    System.out.println("Value : " + childPosition + etChild.getText().toString());
                    Toast.makeText(context, etChild.getText().toString() + "...", Toast.LENGTH_SHORT).show();
                }
            });

            return convertView;
        }

        public int getChildrenCount(int groupPosition) {
            return 1;
        }

        public Object getGroup(int groupPosition) {
            return listItem.get(groupPosition).getValue();
        }

        public int getGroupCount() {
            return listItem.size();
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Nullable
        public View getGroupView(final int groupPosition, boolean isExpanded,
                                 @Nullable View convertView, ViewGroup parent) {
            try {
                if (convertView == null) {
                    LayoutInflater infalInflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.list_group,
                            null);
                }

                TextView txtName = (TextView) convertView.findViewById(R.id.txtName);
                RelativeLayout rlList = (RelativeLayout) convertView.findViewById(R.id.rlList);

                txtName.setText(mArr.get(groupPosition).getValue());
            } catch (Exception e) {
                Logs.d("view- ExListAdapter", e, getClass().getSimpleName());
            }

            return convertView;
        }

        public boolean hasStableIds() {
            return true;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        @Override
        public void onGroupCollapsed(int groupPosition) {
            try {

            } catch (Exception e) {
            }

            super.onGroupCollapsed(groupPosition);
        }
    }

但是一直空白,请伙计帮忙解决它!!!

1 个答案:

答案 0 :(得分:0)

您可以直接从 EditText 获取文字,如下所示

 txtOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                      String text=  etChild.getText().toString();

                      if(!text.isEmpty())
                      Toast.makeText(context,text  + "...",Toast.LENGTH_SHORT).show();


                });