I have a listview filled with some text. But when the listview is empty, it will show a button that takes you to a new activity, where you type some text and it saves on SharedPreferences and finishes. I want the Listview's Activity to update when the other one is closed. I tried onResume() but for some reason it stays refreshing forever. How do I do that?
By the way, I'm updating using:startActivity(getIntent());
finish();
Is there a better way to do it?
Thanks.
EDIT-----------------------------------------------
As @Alex Loper said, I added notifyDataSetOnChange()
in the onResume()
method. But it crashed with this log:
12-31 19:45:05.284 11864-11864/com.nerv.cas.partying E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.nerv.cas.partying, PID: 11864
java.lang.RuntimeException: Unable to resume activity {com.nerv.cas.partying/com.nerv.cas.partying.YourPartiesActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2812)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.nerv.cas.partying.YourPartiesActivity.onResume(YourPartiesActivity.java:48)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1215)
at android.app.Activity.performResume(Activity.java:5327)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2797)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
My class is like this:
ArrayAdapter<String> adapter;
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_parties);
user = getUserData();
fillUserParty(user);
fillAdapter(user);
ListView partiesListView = (ListView) findViewById(R.id.parties_list_view);
Button goToCreatePartyButton = (Button) findViewById(R.id.go_to_create_party_button);
if(adapter != null){
partiesListView.setVisibility(View.VISIBLE);
partiesListView.setAdapter(adapter);
}else{
goToCreatePartyButton.setVisibility(View.VISIBLE);
goToCreatePartyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(YourPartiesActivity.this, CreatePartyActivity.class));
}
});
}
}
@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
private void fillAdapter(User user){
List<Party> parties = user.getParties();
if(parties != null) {
for (int i = 0; i < parties.size(); i++) {
Party party = parties.get(i);
adapter.add(party.getName());
}
}
}
private void fillUserParty(User user){
SharedPreferences preferences = getSharedPreferences("Parties", MODE_PRIVATE);
int numOfParties = CreatePartyActivity.checkNumberOfParties(preferences);
for(int i=0; i<numOfParties; i++){
String partyName = preferences.getString("Name" + i, "");
if(partyName != ""){
int id = preferences.getInt("Id" + i, 0);
Party party = new Party(id, partyName);
user.joinParty(party);
}
}
}
public User getUserData(){
SharedPreferences preferences = getSharedPreferences("User", 0);
int id = preferences.getInt("Id", 0);
String username = preferences.getString("Username", "");
String name = preferences.getString("Name", "");
return new User(id, username, name);
}
I can't find initialize adapter in posted code. I think adapter always is null!
To resolve null pointer exception, change onResume method like this :
@Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.notifyDataSetChanged();
}