将 editText 框的值保存到 Android 上的可扩展列表视图中

Keep values of editText boxes into an expandable list view on Android

我创建了一个可扩展视图,其中每个组下都有一个编辑文本框。

    public class ExpandableActivity extends AppCompatActivity {

        ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        HashMap<String, List<String>> listDataChild;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_expandable);

            expListView = (ExpandableListView) findViewById(R.id.expandableListView);
            expListView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

            // preparing list data
//custom function that populates listDataHeader and listDataChild
            prepareListData(); 

            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
            expListView.setAdapter(listAdapter);

适配器代码:

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private Context context;
        private List<String> listDataHeader;
        private HashMap<String, List<String>> listDataChild;

        public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                     HashMap<String, List<String>> listChildData) {
            this.context = context;
            this.listDataHeader = listDataHeader;
            this.listDataChild = listChildData;
        }


        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            final String childText = (String) getChild(groupPosition, childPosition);

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.e_list_item, null);
            }

            TextView vw_text_sub = (TextView) convertView.findViewById(R.id.esubt);
            EditText vw_edit = (EditText) convertView.findViewById(R.id.eedit);

            vw_text_sub.setText(childText);

            vw_edit.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {            
                        //store value?
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                }

                @Override
                public void afterTextChanged(Editable arg0) {
                }
            });

            return convertView;
        }



        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.e_list_group, null);
            }

            TextView vw_header = (TextView) convertView.findViewById(R.id.eheader);
            vw_header.setTypeface(null, Typeface.BOLD);
            vw_header.setText(headerTitle);

            return convertView;
        }

// other overridden methods here....

    }

由于回收视图的适配器功能,每当我在编辑框中键入一些文本时,当我向上和向下滚动时,此值也会丢失或复制到其他编辑框中。

在列表视图中,一个数组用于存储编辑框的值。

在可扩展列表视图中执行此操作的最佳方法是什么?数组的数组? 您能否提供可扩展列表视图而非列表视图的示例?

编辑:试过了,但我也有问题:

public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.e_list_item, null);
        }

        TextView vw_text_sub = (TextView) convertView.findViewById(R.id.esubt);
        EditText vw_edit = (EditText) convertView.findViewById(R.id.eedit);

        vw_text_sub.setText(childText);
        if(tmp[groupPosition][childPosition] != null) {
            vw_edit.setText(tmp[groupPosition][childPosition]);
        }else{
            vw_edit.setText("");
        }

        vw_edit.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                tmp[groupPosition][childPosition] = arg0.toString();
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });

        return convertView;
    }

tmp 是一个数组 String[10][10]。我的组是 3 并且 children 是一个组中最多 7 个,所以数组不小于我的数据。

问题出在 TestWatcher 上。它多次触发每种方法,我还没有找到原因。所以解决方案是使用 FocusChangeListener 或更好的方法来显示 alertDialog 以填充该字段。然后在肯定按钮上单击我将数据存储在数组中。

所以通过替换 TextWatcher,我设法解决了问题。