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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:33:47+00:00 2026-05-30T23:33:47+00:00

I have two activities. In first I put a list of items with a

  • 0

I have two activities. In first I put a list of items with a ListView. In the second, I want to put a list of items too, but the contents of which will depend on the position of the element, which was pressed in the first Activitу. Here is the code of the first ListView Activity:

public class ListView1 extends Activity implements OnClickListener {
    private ListView lv1;
    private String cats[];

//some code

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

        Bundle bundle = getIntent().getExtras();
        final String category = bundle.getString("category");

        lv1 = (ListView) findViewById(R.id.listView);

            ArrayAdapter<CharSequence> adapter = ArrayAdapter
                    .createFromResource(this, R.array.first_list,
                            R.layout.list_items);
            lv1.setAdapter(adapter);
            cats = getResources().getStringArray(R.array.first_list);

        lv1.setTextFilterEnabled(true);
        lv1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                String defStrID = new Integer(position).toString();

                Intent intent = new Intent();
                intent.setClass(ListView1.this, ListView2.class);

                Bundle b = new Bundle();
                b.putString("defStrID", defStrID);
                intent.putExtras(b);

                startActivity(intent);
            }
        });
    }
}

This is the code which gets position of element in the list:

String defStrID = new Integer(position).toString();

I get it in the second ListView with:

Bundle bundle = getIntent().getExtras();
String elementID = bundle.getString("defStrID");

and I have some arrays in arrays.xml for the second ListView. For example:

R.array.category1
R.array.category2
R.array.category3
R.array.category4

or arrays like this (it’s not very important):

String category1[] = { //some items here }
String category2[] = { //some items here }
etc.

As an example, only a few arrays, actually more, so I do not want to use the if else construction. How to do it another way, I do not know. The second ListView activity will be the same as the first one, except for the described above.


Second activity:

public class List2 extends ListActivity {
    private ListView lv1;
    private String category1[] = {"Name1", "Name2"};
    private String category2[] = {"Surname1", "Surname2"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        ;
        String elementID = bundle.getString("defStrID");
        String arrayName = "category" + elementID;
        int id = getResources().getIdentifier(arrayName, "array",
                this.getPackageName());
        Log.v("LOOK_FOR_ME", "category" + " and the id i got is " + id);
        String[] stats = getResources().getStringArray(id);
        lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_items,
                stats));
    }
}
  • 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-30T23:33:49+00:00Added an answer on May 30, 2026 at 11:33 pm

    In your second Activity get the id from the other activity:

    Bundle bundle = getIntent().getExtras();
    String elementID = bundle.getString("defStrID");
    

    If your arrays have the name categoryX(x -number) then build a String with the array name:

    String arrayName = "category" + elementID;
    

    Then you can get the id of that particular array like this:

    int id = getResources().getIdentifier(arrayName, "array", getPackageName());
    

    and finally get the String array and put it in the adapter:

    String[] stats = getResources().getStringArray(id);
    

    Also keep in mind that the position in the list start at 0 so name your array starting with 0;

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

Sidebar

Related Questions

I have two activities. In first I come to the second activity from first
I have two activities. The first activity display list of the users with short
I have two activities; Home is my first activity and Settings is my second
I'm new to Android. I have two activities. First activity shows selected contact list
I have two different activities. The first launches the second one. In the second
I have two activities. The first one executes the second one. Intent i =
I have two activities but I want to apply a condition on application start,
I have two activities and the first is generating properly however the second is
I have two activities with ListView. This two activitises have individual layout-file. First have
I have two Activities and I want to keep a reference of a second

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.