我在片段中实现了2个listViews和editText。重新加载ListViews,例如当用户在editText中输入内容或从左侧listView中选择行时。但是当我最小化我的应用程序并在1小时后最大化时,每个列表视图有2个,editText中有2个不同的数字(重叠),一个来自之前的状态,一个来自new。我该如何解决?在onPause
方法中删除适配器并在onStart
中重新加载并从editText中保存值?是更简单的解决方案吗?
在第一个listview上设置监听器以重新加载第二个:
listViewFrom.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listViewFrom.setItemChecked(position, true);
currentUnit = ((FromAdapter.ViewHolder) view.getTag()).getUnit();
createToList(inputEditText.getText().toString(), currentUnit);
}
});
添加侦听器以编辑文本:
inputEditText.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) {
}
@Override
public void afterTextChanged(Editable s) {
createToList(s.toString(), currentUnit);
}
});
和createToList函数:
private void createToList(String string, Unit unit) {
if (isNumeric(string)) {
double value = Double.valueOf(string);
List<Unit> list = createList(value, unit);
ToAdapter toAdapter = new ToAdapter(list, getActivity());
listViewTo.setAdapter(toAdapter);
}
}
XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_marginBottom="5dp"
android:id="@+id/inputEditText"
android:inputType="numberDecimal"
android:maxLength="10"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/inputEditText"
android:orientation="horizontal">
<ListView
android:id="@+id/fromListView"
android:choiceMode="singleChoice"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4">
</ListView>
<ListView
android:id="@+id/toListView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.6">
</ListView>
</LinearLayout>
它看起来如何: