Ein einfaches ListActivity
enthält TextView
und ListView
Objekte funktioniert gut, wenn der Bildschirm nicht rotiert.Android ListActivity: onCreate wird jedes Mal nach der Bildschirmdrehung aufgerufen, das ausgewählte Element wird zurückgesetzt, aber visuell überprüft
Problembeschreibung. Wenn die App zum ersten Mal ausgeführt wird, zeigt TextView myText
-1
an. Es klappt. Wenn Sie auf verschiedene Elemente klicken, zeigt myText
den Wert der ausgewählten Artikel-ID (oder Position) an. Also, es funktioniert auch. An einem bestimmten Moment, wenn zum Beispiel Artikel Two
ausgewählt und myText
zeigt 2
ich drehen Sie den Bildschirm und myText
wird -1
(weil onCreate
wieder auslöst), so bedeutet es, die ListView myList
Auswahl löscht, sondern Element Two
bleibt ausgewählt (als gemalt RadioButton aktiviert). Was ist das Problem? (P.S. Das Gleiche passiert mit GridView).
Hier ist meine Quelle-Code:
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".TestActivity">
<include layout="@layout/content_test" />
</android.support.design.widget.CoordinatorLayout>
content_test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity">
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/myText"
android:choiceMode="singleChoice"/>
</RelativeLayout>
TestActivity.java
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class TestActivity extends ListActivity {
static String[] items = {"Zero", "One", "Two"};
TextView myText;
ListView myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
myList = (ListView) findViewById(android.R.id.list);
setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_single_choice, items));
myText = (TextView) findViewById(R.id.myListViewSelectedText);
myText.setText(String.valueOf(myList.getSelectedItemPosition()));
}
@Override
public void onListItemClick(ListView parent, View view, int position, long id) {
myText.setText(String.valueOf(position));
}
}
Schlechte Idee. Wie die Dokumentation sagt - es ist die letzte Option und die Verwendung dieses Attributs sollte vermieden werden. Weitere Informationen finden Sie hier: [Aktivität] (https://developer.android.com/guide/topics/manifest/activity-element.html) find configChanges Beschreibung. – 3mpty