Welche Daten in onResume() aktualisieren?

G

Garnet

Ambitioniertes Mitglied
1
Ich habe folgendes Problem: Ich habe eine ListView, deren Einträge man als Favoriten speichern kann. Es gibt dazu eine Klasse Favoritenliste und eine weitere Favoritendetails, in der zu jedem Listeneintrag eine Detailansicht angezeigt wird. In Favoritendetails kann man die Favoriten löschen. Wenn ich jetzt einen Favorit lösche und kehre mit dem Back-Button in die Liste zurück soll auch der entsprechende Listeneintrag gelöscht sein.
Soweit ich das verstanden habe, muss ich dazu die onResume() Methode überschreiben. Nur leider weiß ich nicht genau, was genau ich da jetzt reinschreiben muss.

Hier mein Code der Klasse Favoritenliste:

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Favoritenliste [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ListActivity[/SIZE][/LEFT]
 
[LEFT][SIZE=2]{[/SIZE]

[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<HashMap<String, String>> [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myListing[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<HashMap<String, String>>();[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] SimpleAdapter [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]saList[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]/** Called when the activity is first created. */[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) [/SIZE]

[SIZE=2]{[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]listview_anzeige[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][SIZE=2]String[] speaker = getResources().getStringArray(R.array.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sprecher_array[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]

[SIZE=2]String[] program = getResources().getStringArray(R.array.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]vortragsthema_array[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[LEFT][SIZE=2]String[] time = getResources().getStringArray(R.array.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]zeit_array[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String titel = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].getString(R.string.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]favoritenuebersicht[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]

[SIZE=2]((TextView)(findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]textView2[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]))).setText(titel);[/SIZE][/LEFT]

 
 
 
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]saList[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]= [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] SimpleAdapter([/SIZE]

[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2],[/SIZE]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myListing[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE]
[SIZE=2]R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]dreizeiliges_item[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2],[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String[] { [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"line1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"line2"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"line3"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] },[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] { R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]text1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]text2[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]text3[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2] } );[/SIZE]
[SIZE=2]setListAdapter( [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]saList[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
[/LEFT]

 
 
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] value = StartseiteActivity.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]favoritausgewaehlt[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2];[/SIZE][/LEFT]
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] i=0; i<speaker.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]length[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; i++) [/SIZE]

[SIZE=2]{[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (value[I]==1)[/I][/SIZE]
[I][SIZE=2]{[/SIZE][/I]
[I][SIZE=2][U]HashMap[/U] item = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] [U]HashMap[/U]();[/SIZE][/I]
[I][SIZE=2][U]item.put([/U][/SIZE][U][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"line1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], program[I] )[/I][/SIZE][/U][SIZE=2][I];[/I][/SIZE][/I]
[I][I][SIZE=2][U]item.put([/U][/SIZE][U][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"line2"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], speaker[I])[/I][/SIZE][/U][SIZE=2][I];[/I][/SIZE][/I][/I]
[i][I][I][SIZE=2][U]item.put([/U][/SIZE][U][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"line3"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], time[I])[/I][/SIZE][/U][SIZE=2][I];[/I][/SIZE][/I][/I][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][I][I][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myListing[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].add([U]item[/U]);[/SIZE][/I][/I][/I][/LEFT]

[I][LEFT][I][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]saList[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].notifyDataSetChanged();[/SIZE][/I][/I][/I]
[LEFT][I][I][I][SIZE=2]}[/SIZE][/I][/I][/I][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][I][I][I][SIZE=2]}[/SIZE][/I][/I][/I][/LEFT]
 
 
 
[LEFT][I][I][I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ListView lv = getListView(); [/SIZE][/I][/I][/I][/LEFT]

[I][LEFT][I][I][SIZE=2]lv.setTextFilterEnabled([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]true[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]); [/SIZE][/I][/I][/I]
[LEFT][I][I][I][SIZE=2]lv.setOnItemClickListener([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] OnItemClickListener() [/SIZE][/I][/I][/I][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][I][I][I][SIZE=2]{ [/SIZE][/I][/I][/I][/LEFT]

[I][LEFT][I][I][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//[U]itemposition[/U] in [U]Favoritenliste[/U] [U]ist[/U] [U]nicht[/U] [U]gleich[/U] [U]wie[/U] die in [U]der[/U] [U]Programmliste[/U][/COLOR][/SIZE][/COLOR][/SIZE][/I][/I][/I]
[LEFT][I][I][I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onItemClick(AdapterView<?> parent, View view, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] position, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]long[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] id) [/SIZE][/I][/I][/I]
[I][I][I][SIZE=2]{[/SIZE][/I][/I][/I]
[I][I][I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] originalitempos = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][16];[/SIZE][/I][/I][/I]
[I][I][I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] value = StartseiteActivity.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]favoritausgewaehlt[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2];[/SIZE][/I][/I][/I]
[I][I][I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] a = -1;[/SIZE][/I][/I][/I]
[I][I][I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] i = 0; i<value.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]length[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; i++)[/SIZE][/I][/I][/I]
[I][I][I][SIZE=2]{[/SIZE][/I][/I][/I][/LEFT]
[/LEFT]

 
 
 
 

[i][I][LEFT][I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (value[I]==1)[/I][/SIZE][/I][/LEFT]
[/I]

[I][LEFT][I][I][SIZE=2]{a = a+1;[/SIZE][/I][/I][/I]
[LEFT][I][I][I][SIZE=2]originalitempos[a] = i;}[/SIZE][/I][/I][/I][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][I][I][I][SIZE=2]}[/SIZE][/I][/I][/I][/LEFT]
 
 
 
[LEFT][I][I][I][SIZE=2]String [U]itemposition[/U];[/SIZE][/I][/I][/I][/LEFT]

[I][LEFT][I][I][SIZE=2]Intent intent = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Intent();[/SIZE][/I][/I][/I]
[LEFT][I][I][I][SIZE=2]intent.setClassName(getPackageName(), getPackageName()+[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]".Programmdetails"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/I][/I][/I]
[I][I][I][SIZE=2]position = originalitempos[position];[/SIZE][/I][/I][/I]
[i][i][i][SIZE=2]intent.putExtra([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"selected"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], itemposition =Long.[I]toString[/I](lv.getAdapter().getItemId(position))); [/SIZE][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][SIZE=2]startActivity(intent);[/SIZE]

[SIZE=2]}[/SIZE]
[LEFT][SIZE=2]}); [/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
[/LEFT]

 
 

[LEFT][SIZE=2]}[/SIZE][/LEFT]
 
Zuletzt bearbeitet:
Du könntest eine public Methode bauen, die die Funktionalität zum Befüllen des ListAdapters beinhaltet. Diese rufst Du beim Start der ListActivity in der onCreate auf.

In den Details überschreibst Du die onbackpressed und rufst hier, bevor Du zurückspringst, die Methode zum Befüllen des ListAdapters wieder auf, damit sich dieser die Werte neu holt.
 
  • Danke
Reaktionen: Garnet
Jetzt hab ich noch ein kleines Problem. Wie rufe ich in den Details die Methode zum Befüllen auf? Wenn ich die Methode static mache, bekomm ich Fehlermeldungen, z.B. "Cannot make a static reference to the none static field speaker".
 
Kein Problem:

Wenn es sich bei den Details ebenfalls um eine Activity handelt, dann rufst Du sie über ein Intent ganz normal auf, beendest die ListActivity aber mit einem finish():

Code:
startActivity(new Intent(this, DetailsActivity.class));
    	
finish();

Wenn Du in der DetailActivity mit der Back-Taste zurück springst, wird die OnCreate ausgeführt und der Adapter wird neu geladen.


Eine andere Möglichkeit ist es die startActivityForResult(...) statt startActivity(...) zu verwenden. Dann kannst Du die Methode zum Befüllen des ListAdapter in der OnActivityResult der ListActivity aufrufen.

Handelt es sich bei den Details um einen Dialog, so kannst Du dem Dialog einfach die "parentActivity" im Konstruktor mitgeben und darüber die "befuelleAdapter-Methode" aufrufen. Oder Du hängst dem Dialog einen OnDismiss-Listener an und machst das dort.

Da gibt es mehrere Lösungsansätze. Static brauchst Du die Methode nicht deklarieren.
 
  • Danke
Reaktionen: Garnet
Danke für die ausführliche Antwort.
Also bei Details handelt es sich auch um eine Activity. Ich hab jetzt das mit dem finish() probiert, da ist es jetzt aber so, wenn ich den Back-Button drücke geht er überhaupt nicht mehr auf die ListActivity zurück, sondern auf die Activity vor der ListActivity.
 
Du musst bei der onbackpressed die ListActivity ebenfalls über ein Intent aufrufen. Also wie oben in meinem Code-Sample.
 
Ok das hab ich jetzt noch gemacht. Leider gibts da schon wieder ein neues Problem. Wenn ich jetzt auf den Back-Button drücke geht er auf die Liste zurück und es ist auch richtig aktualisiert. Wenn ich dann aber nochmals den Back-Button drücke geht er wieder in die Detailansicht anstatt in die Activity die vor der Listenansicht kommt.

Ok, ich glaub ich habs jetzt hinbekommen, hab jetzt in beide Klassen die onBackPressed() geschrieben, dann scheints zu funktionieren.

Der ursprüngliche Beitrag von 17:59 Uhr wurde um 18:38 Uhr ergänzt:

Sorry, hab doch noch ein Problem. Und zwar erreicht man meine Detailsklasse einmal über die Favoritenliste und einmal über die normale Liste. Bei onbackpressed kann ich ja jetzt nur eine von den beiden Klassen angeben. Kann man da auch irgendwie sagen, er soll auf die Activity zurückgehen, die als letztes aufgerufen wurde? Oder wie könnte man das lösen?
 
Klaro, Du gibst der Activity über putExtra z.B. einen String oder eine Id mit. Ich habe sowas mal über eine Enumeration gelöst.

Bei der OnCreate der DetailActivity ließt Du das Extra aus und speicherst es in eine Variable. Bei der onbackpressed kannst Du dann mit equals prüfen, welche Activity das Parent ist und dementsprechend initialisierst Du das Intent.
 
  • Danke
Reaktionen: Garnet

Ähnliche Themen

A
Antworten
10
Aufrufe
1.021
swa00
swa00
A
Antworten
10
Aufrufe
1.919
swa00
swa00
D
Antworten
23
Aufrufe
2.533
Data2006
D
Zurück
Oben Unten