简体   繁体   中英

How to get updated data from an EditText in an ExpandableListView?

I am a beginner Android Developer, and I have a problem with a textData in EditText from Expandable ListView;

So, I am using SimpleExpandableListViewAdapter. In the custom childLayout I have an EditText, which uses data from the dataBase as a text. In the Main Activity user can change the data and then he presses a button "Apply Changes". But in button.OnClickListener I can get listView EditTexts from an adapterValues, that are not changed. So, every time listView displays the same data.

I mean, EditText.getText() gives me old data;

I need to update the Adapter every time button pressed, but I don't have new data.

The question is, how to get current EditText text in Expandable ListView? Is there a way to get each EditText value without an adapter pointer?

Sorry, I cannot share all my code, but this is a root of my problem:

This code is not working:

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) {

                }
            });
        }
    });

"Focus Changed" is there, but Log "new Temp" is not showing up.

Here is my Adapter:

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

Thank you.

You are setting "Focus Changed" for the ExpandableListView , not the EditText.

Try extend the adapter to override getChildView() and find the EditText, like this:

    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;
        }
    };

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM