Я новичок в кодировании android, поэтому я уверен, что в моем коде должна быть какая-то глупая ошибка. Я пытаюсь сделать приложение TO DO LIST (без использования Listview), и я не знаю, как обрабатывать изменение ориентации в Android. Когда я поворачиваю устройство, все мои данные теряются. Если вы можете предложить, как я могу это сделать, я буду очень благодарен.Как справиться с изменением ориентации экрана в android без onConfigChanged
Вот мой исходный код:
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout mlistlayout;
String toDo;
private ArrayList<Entry> mlist;
static class Entry {
String S;
boolean b;
public Entry(String S, boolean b) {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlistlayout = (LinearLayout) findViewById(R.id.list);
mlist = (ArrayList<Entry>) getLastCustomNonConfigurationInstance();
if (mlist == null) {
mlist = new ArrayList<>();
}
Button sub = (Button) findViewById(R.id.buttonsubmit);
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText1 = (EditText) `
findViewById(R.id.editText1);
toDo = editText1.getText().toString().trim();
if (toDo.isEmpty()) {
return;
}
editText1.setText("");
AddListEntry(toDo);
}
});
}
@Override
public Object onRetainCustomNonConfigurationInstance() {
return mlist;
}
void AddListEntry(String S) {
LayoutInflater inflater = LayoutInflater.from(this);
View v = inflater.inflate(R.layout.row, mlistlayout, false);
final Entry entry = new Entry(S, false);
TextView t = (TextView) v.findViewById(R.id.task_title);
t.setText(toDo);
CheckBox cb=(CheckBox) v.findViewById(R.id.task_delete);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
entry.b=isChecked;
}
});
mlist.add(entry);
mlistlayout.addView(v);
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bag"
tools:context="calpoly.edu.todolist.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:inputType="text"
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:layout_weight="2" />
<Button
android:id="@+id/buttonsubmit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/submit" />
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/list"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"></LinearLayout>
</ScrollView>
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linear_layout"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/task_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="20sp" />
Вы слышали о saveInstanceState? –
Да, но я не знаю, как его использовать:/Я знаю, что он использует пакет, и код должен быть сериализуемым. но я не знаю, как его реализовать – codelover