首页 > 解决方案 > 如何从 ExpandableListView 中的 EditText 获取更新的数据?

问题描述

我是一名初学者 Android 开发人员,我在 Expandable ListView 的 EditText 中遇到了 textData 的问题;

所以,我正在使用 SimpleExpandableListViewAdapter。在自定义 childLayout 中,我有一个 EditText,它使用数据库中的数据作为文本。在主活动中,用户可以更改数据,然后按下“应用更改”按钮。但是在 button.OnClickListener 中,我可以从 adapterValues 中获取 listView EditTexts,这些没有改变。因此,每次 listView 显示相同的数据。

我的意思是, EditText.getText() 给了我旧数据;

每次按下按钮时我都需要更新适配器,但我没有新数据。

问题是,如何在 Expandable ListView 中获取当前的 EditText 文本?有没有办法在没有适配器指针的情况下获取每个 EditText 值?

抱歉,我不能分享我所有的代码,但这是我的问题的根源:

此代码不起作用:

TimersExpandableListView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {  
            Log.w("editText", "focus changed");  
            DailyTimersTemperatureEditText = v.findViewById(R.id.timers_daily_child_degrees_editText);

            DailyTimersTemperatureEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    String newTemperature = DailyTimersTemperatureEditText.getText().toString();
                    Log.w("new temp", newTemperature);
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    });

“焦点改变”在那里,但日志“新温度”没有出现。

这是我的适配器:

 final SimpleExpandableListAdapter DailyTimersListAdapter = new 
 SimpleExpandableListAdapter(context, // this.getContext();
            DailyGroupDataList,
            R.layout.timers_daily_list_parent_layout,
            DailyGroupFrom, GroupDailyTo, //Array of Hashmaps, ParentLayout Array;
            NightAndDayChildData, R.layout.timers_daily_list_child_layout,
            new String[]{"DayAndNightChildTime", "DayAndNightTemp"},
            new int[]{R.id.timers_daily_child_time_textView, R.id.timers_daily_child_degrees_editText}
    );
    TimersExpandableListView.setAdapter(DailyTimersListAdapter);

谢谢你。

标签: androidlistviewandroid-edittextexpandablelistview

解决方案


您正在为ExpandableListView设置“焦点已更改” ,而不是 EditText。

尝试扩展适配器以覆盖 getChildView() 并找到 EditText,如下所示:

    final SimpleExpandableListAdapter DailyTimersListAdapter = new
            SimpleExpandableListAdapter(context, // this.getContext();
            DailyGroupDataList,
            R.layout.timers_daily_list_parent_layout,
            DailyGroupFrom, GroupDailyTo, //Array of Hashmaps, ParentLayout Array;
            NightAndDayChildData, R.layout.timers_daily_list_child_layout,
            new String[]{"DayAndNightChildTime", "DayAndNightTemp"},
            new int[]{R.id.timers_daily_child_time_textView, R.id.timers_daily_child_degrees_editText}
    ){
        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            View childView = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
            EditText DailyTimersTemperatureEditText = childView.findViewById(R.id.timers_daily_child_degrees_editText);
            DailyTimersTemperatureEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if(!b){
                        Log.w("new temp pos", "Save data, group: " + groupPosition + ", child: " + childPosition);
                        String newTemperature = ((EditText)view).getText().toString();
                        Log.w("new temp data", newTemperature);

                        // Code to save the new data.

                    }
                }
            });
            return childView;
        }
    };

希望有帮助!


推荐阅读