Android 如何在可展开列表视图中从EditText获取文本

Android 如何在可展开列表视图中从EditText获取文本,android,android-edittext,expandablelistview,Android,Android Edittext,Expandablelistview,我有一个可扩展的listview,其中包含作为子对象的edittext,在这个可扩展的listview下面我有一个按钮,当用户单击按钮时,我想从edittext获取文本并存储在字符串中,我在getChildView方法中尝试了下面的代码 MyAdapter public class ExListAdapter extends BaseExpandableListAdapter { private Activity context; private Map<

我有一个可扩展的listview,其中包含作为子对象的edittext,在这个可扩展的listview下面我有一个按钮,当用户单击按钮时,我想从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);
        }
    }
公共类ExListAdapter扩展了BaseExpandableListAdapter{
私人活动语境;
私人地图收藏;
私有ArrayList列表项;
私有文本视图txtOk;
公共ExListAdapter(活动上下文、ArrayList listItem、,
地图集合,文本视图(txtOk){
this.context=上下文;
this.collections=集合;
this.listItem=listItem;
this.txtOk=txtOk;
}
公共对象getChild(int-groupPosition,int-childPosition){
返回1;
}
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@可空
公共视图getChildView(最终int groupPosition,最终int childPosition,
布尔值isLastChild、@Nullable View convertView、ViewGroup父级){
LayoutInflater充气器=上下文。getLayoutInflater();
if(convertView==null){
convertView=充气机。充气(R.layout.list\u子项,空);
}
etChild=(EditText)convertView.findViewById(R.id.etChild);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
etChild.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
//TODO自动生成的方法存根
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
System.out.println(“值:”+childPosition+etChild.getText().toString());
Toast.makeText(上下文,etChild.getText().toString()+“…”,Toast.LENGTH_SHORT.show();
}
});
etChild.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
v、 getParent().RequestDisallowWinterCeptTouchEvent(true);
开关(event.getAction()&MotionEvent.ACTION\u掩码){
case MotionEvent.ACTION\u UP:
v、 getParent().RequestDisallowWinterCeptTouchEvent(false);
打破
}
返回false;
}
});
etChild.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
//TODO自动生成的方法存根
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
System.out.println(“值:”+childPosition+etChild.getText().toString());
Toast.makeText(上下文,etChild.getText().toString()+“…”,Toast.LENGTH_SHORT.show();
}
});
返回视图;
}
公共整数getChildrenCount(整数组位置){
返回1;
}
公共对象getGroup(int-groupPosition){
返回listItem.get(groupPosition.getValue();
}
public int getGroupCount(){
返回listItem.size();
}
公共长getGroupId(int-groupPosition){
返回组位置;
}
@可空
公共视图getGroupView(最终int-groupPosition,布尔值isExpanded,
@可为空的视图(视图、视图组父视图){
试一试{
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)上下文
.getSystemService(上下文布局\充气机\服务);
convertView=充气机充气(R.layout.list\U组,
无效);
}
TextView txtName=(TextView)convertView.findViewById(R.id.txtName);
RelativeLayout rlList=(RelativeLayout)convertView.findViewById(R.id.rlList);
setText(mArr.get(groupPosition.getValue());
}捕获(例外e){
Logs.d(“view-ExListAdapter”,e,getClass().getSimpleName());
}
返回视图;
}
公共布尔表ID(){
返回true;
}
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
@凌驾
公共void-ongroupposition(int-groupPosition){
试一试{
}捕获(例外e){
}
super.ongroupposition(groupPosition);
}
}

但一直都是空白,请帮我解决!!!

你可以直接从编辑文本中获取文本,如下所示

 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();


                });

你不是简单地得到像“etChild.getText().toString()”这样的文本吗?没有“a”