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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:48:06+00:00 2026-06-01T04:48:06+00:00

ListViews have always been my weak point and right now I am practicing putting

  • 0

ListViews have always been my weak point and right now I am practicing putting a Listview, within a Listview. Anyway, I first call my ListView at the start of my program and it loads it with an array saved in my strings.xml:

String[] departments = getResources().getStringArray(
                R.array.departments_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
                departments));
        setContentView(R.layout.main);

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

What I want to do is update this ListView with a new array of values each time a list item is clicked. The reason why I am trying to do it this way is because I plan on having 27 different arrays with different values for each position, and I feel it would be lighter on my resources if instead of making a ListView for each array of items, I would update this one ListView. I know I am probably not doing this the most efficient way, but if there is another way of implementing my idea please tell me.

   lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // When clicked, show a toast with the TextView text
                    switch (position) {
                    case 0:
                        try {


    //It is here that i dont know what to do, I was going to call 
//the Listview the same way i did previously using my setlistadapter, 
//but i kept getting errors about the code being undefined


                            String[] listitems1 = getResources().getStringArray(
                                    R.array.items_array);

                        } catch (ClassCastException e) {
                            Toast.makeText(getApplicationContext(), "Error",
                                    Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 1:
                        try {
                      //The listview will be changed again here
                        } catch (ClassCastException e) {
                            Toast.makeText(getApplicationContext(), "Error",
                                    Toast.LENGTH_SHORT).show();
                        }
                        break;
                    }
                };

            });
  • 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-01T04:48:08+00:00Added an answer on June 1, 2026 at 4:48 am

    Your approach is wrong( if I understand what are you doing). Instead of replacing the adapter of the ListView every time the user clicks(and simply setting a new adapter should work) a element in the initial list you should start a new activity passing the clicked position and in your new activity set the adapter on a ListView with the correct array based on that position.

    A small example:

    Main class:

    /**
     * The main class with the initial 27 items array.
     */
    public class Class1 extends ListActivity {
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            // start the second activity that will show our array depending on the
            // position clicked
            Intent i = new Intent(this, Class2.class);
            // put the position in the Intent so we can know in the other activity
            // what array to load.
            i.putExtra("pos", position);
            startActivity(i);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // I just used a simple array of 2 items, you'll load your 27 items
            // array
            String[] items = { "1", "2" };
            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, items));
        }
    
    }
    

    Secondary activity that will show the array based on the previously selected position:

    public class Class2 extends ListActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // get the Intent that started the activity
            Intent i = getIntent();
            // find out what position did that other activity send to us.
            int position = i.getIntExtra("pos", -1);
            // load the ListView with an adapter based on the array that you
            // want(according to that position)
            if (position == 0) {
                // the first element in the main list
                String[] items = getResources().getStringArray(R.array.a1);
                setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, items));
            } else if (position == 1) {
                // the second element in the main list
                String[] items = getResources().getStringArray(R.array.a2);
                setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, items));
            } else {
                // etc
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF ListView that should be extended with an always visible footer.
I have been working with listviews and arrays, what I am struggling with is
I have another problem with ListView :( Now I need to move items in
This has been killing me for two days now. I have a main Activity
This has been killing me for two days now. I have a main Activity
I have a listview with many values. I want my first row to be
I've got a listbox from which I'm dragging into the ListView. Now I have
I have two listviews, one where the data is displayed and another where I
I have a RelativeLayout with different elements. I was planning to have two ListViews
The main concept goes like this. I have four listviews with its own data

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.