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 3788698
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:02:25+00:00 2026-05-19T12:02:25+00:00

I want to show an empty list view, which is then populated by user

  • 0

I want to show an empty list view, which is then populated by user input. I have the UI flow working, and I populate a list of my custom objects after the user enters some information via a view which is invoked through setContentView (i.e. no a new Activity).

I take the input and add it to a list, which I want to be summarised on the ListView. However, whenever I add to the list and/or the ArrayAdapter and call adapter.notifyDataSetChanged() it does not do what I want. The ListView is still empty. Argh! It’s driving me insane!

@Override
public void onCreate(Bundle blah) {
    ListView listView = (ListView) findViewById(R.id.results_list);
    listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.mnu_add:
        final Activity act = this;
        setContentView(R.layout.record_details);// the sub-view that takes the user input
        // the button on the form to 'add' details:-
        ((Button) findViewById(R.id.recored_details_add_btn))
        .setOnClickListener(
            new OnClickListener() {
            public void onClick(View v) {
                    // get input from widgets 
                    list.add(someObject);
                    ((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
                    setContentView(R.layout.list_view);

                }
            }
        );
        ((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
        break;
    }
    return true;
}

Please, save me from my misery and inform me of my stupidty?

Thanks in advance.

  • 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-05-19T12:02:26+00:00Added an answer on May 19, 2026 at 12:02 pm
    public void onClick(View v) {
        // get input from widgets 
        list.add(someObject);
        ((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
        setContentView(R.layout.list_view);
    

    Is it possible that this setContentView in the onClick handler is creating a new instance of the list view widget (with no adapter) or reinitializing the list view (clearing the adapter)?

    Try putting something in the list initially in onCreate and then see if it disappears when you hit the button.

    I haven’t seen any code (although I’m a relative newbie) that switches views within the activity’s lifetime to bring up essentially bring up different pages – most use a separate activity.

    Edit:

    OP asks:

    Thanks…So how can I get what I want? The list I’m backing the adapter with is static; should I just use activities instead and rely on onCreate loading from the static field?

    Some options:

    1. Use separate activities

    2. Re-associate the adapter (call setAdapter again) – probably a bad idea

    3. Declare both layouts in the same file. You’ll hide one and unhide the other to switch between views rather can calling setContentView. This is similar to how ListView layout works (one for when the list is empty and one for when it is not). I think I’ve seen an example of this somewhere on the net, but I don’t have a reference right now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grid view which is used to show the Product details. Another
I want to be able to show an alternative to an empty list, currently
I want show annotation view (above one of my pin) after load map. For
I have an index page which shows a paged list of data from a
What I want to do is quite simple: I have a model which I
I have implement FragmentPagerAdapter in my app but it show only a same list
I have customized a combobox to show line types, in the selection list, but
I'm struggling implementing a custom validator in my Android app. I want to show
I have a web site which enables user to add or delete their own
I have got a huge list of tasks loaded on the start. I want

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.