android - 如何从 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);
谢谢你。
解决方案
您正在为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;
}
};
希望有帮助!
推荐阅读
- c# - 如何从arduino检索值到asp.net
- optimization - Dart 2 中的最佳渲染循环是什么?
- postgresql - 使用 SQLX 转到可选字段
- apache-spark - Spark Kafka 在 spark 2.3.0 中使用 python 流式传输
- java - 无法使用 Selenium WebDriver 上传许可证文件
- ms-access - Microsoft Access:查询以查找数据差异
- entity-framework - EF Core - 阅读器关闭时调用 CheckDataIsReady 的尝试无效(OutOfMemory)
- python-3.x - Python 3 - Base64 编码
- python - 如何在python中按AaB而不是ABa对字符串进行排序
- python - 使用 Python3 读取两个文件并写入一个文件