Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9102617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:25:36+00:00 2026-06-17T01:25:36+00:00

I have a problem with list refresh when user closes one activity and returns

  • 0

I have a problem with list refresh when user closes one activity and returns to previous. I see that problem is very common but I can’t solve it.

I overridden onResume method:

@Override
public void onResume() {
    super.onResume();
    populateList();
}

populateList() is a method where I populate listView with list of Strings:

arrayAdapter = new CustomArrayAdapter(this, R.layout.symbol_item,list);
listView.setAdapter(arrayAdapter);

The problem is that when second activity is closed, new items are just added again in the ListView so I have every item doubled. Like it’s not refreshed.

If I put notifyDataSetChanged() in onResume() it throws me nullPointerException because when activity is started first time there is no adapter initialized when activity is first time started.

I’m not sure how to handle this.

public class testActivity extends Activity {


    private int id=1;
    private ListView listView;
    private CustomArrayAdapter arrayAdapter;
    private ArrayList<String> list = new ArrayList<String>();
    ArrayList<Item> objectList = new ArrayList<Item>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

    }

    @Override
    public void onResume() {
        super.onResume();
        populateList();
    }

    private void populateList() {
        try {
            objectList = new GetAsyncTask(id).execute();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int size = objectList.size();
        String name;

        for (int i = 0; i < size; i++) {
            name = objectList.get(i).getName();
            list.add(name);
        }

        arrayAdapter = new CustomArrayAdapter(this, R.layout.symbol_item,
                list);
        listView.setAdapter(arrayAdapter);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:25:37+00:00Added an answer on June 17, 2026 at 1:25 am

    well right off the bat, you could easily knock this away with a simple conditional statement that performs the command only if the adapter isn’t null:

        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    

    But this seems to me like, at a deeper level, your code could be re-factored somewhat to be more efficient, though not necessarily more functional.


    do it like this:

    private int id = 1;
    private ListView listView;
    private CustomArrayAdapter arrayAdapter;
    private ArrayList<String> list = new ArrayList<String>();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        populateList();
    }
    
    private void populateList() {
    
        ArrayList<Item> objectList;
        try {
            objectList = new GetAsyncTask(id).execute();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    
        list.clear();
        for (int i = 0; i <objectList.size(); i++) {
            String name = objectList.get(i).getName();
            list.add(name);
        }
        if (arrayAdapter == null) {
            arrayAdapter = new CustomArrayAdapter(this, R.layout.symbol_item, list);
            listView.setAdapter(arrayAdapter);
        } else {
            arrayAdapter.notifyDataSetChanged();            
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have one problem with handling list,i have three class named as UserInf,userData,userProcess,i created
I have a problem with populating an autocomplete list based on a previous input.
I have a problem with list and listview. I started a project and added
I have the following problem: list.c struct nmlist_element_s { void *data; struct nmlist_element_s *next;
I have problem adding arraylist to list view, will explain about my problem here..
I have a problem displaying zebra list after filtering results. I have a zebra
I have a problem with an list-component on the sencha touch 2 framework. My
I have problem with keeping my model (represented as List) in sync with a
i have a list view with a BaseAdapter for custom view.. i have problem
I am wondering why I have this problem with my list. I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.