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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:49:29+00:00 2026-06-18T00:49:29+00:00

I have a question regarding performance and best approach for my Android code. What

  • 0

I have a question regarding performance and best approach for my Android code.

What I need to do is fairly simple, I want to dynamically assign a text value to a string ressource, depending on a int parameter:

For now I am using a big switch case

int messagesCategory;
if(extras !=null) {
        messagesCategory = extras.getInt("category");
    }

TextView titleText;
    titleText = (TextView) findViewById(R.id.headerTitle);

switch (messagesCategory) {
    case 1: titleText.setText(R.string.TitleMessageList1); break;
    case 2: titleText.setText(R.string.TitleMessageList2); break;
    case 3: titleText.setText(R.string.TitleMessageList3); break;
    case 4: titleText.setText(R.string.TitleMessageList4); break;

    case ...: titleText.setText(R.string.TitleMessageList...); break;

    case n: titleText.setText(R.string.TitleMessageListn); break;
    default: titleText.setText("a default title");  break;
    }

Let’s say I have 30 lines in this switch…
It works but for many cases, it looks there is a better way to achieve this.
Unfortunately it does not look possible to assign something dynamic to R.string..

So my first question is: 1) performance wise, in this case, is it a problem to use a big switch for 30 cases or so?
2) what should be the best approach?

Thanks and have a good day

  • 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-18T00:49:30+00:00Added an answer on June 18, 2026 at 12:49 am

    Simple,

    int[] stringIds = {R.string.TitleMessageList1, R.string.TitleMessageList2,...};
    
    int messagesCategory;
    TextView titleText = (TextView) findViewById(R.id.headerTitle);
        if(extras !=null) {
                messagesCategory = extras.getInt("category");
                if(messagesCategory  <= n)
                titleText.setText(stringIds[messagesCategory]);
                else  titleText.setText("a default title"); 
     }
    

    Now, there is no switch-case and comparison,, Basic Java and Android fundamentals make it to easy and short..

    As R.string.XXX is an int value generated in R.java file you have to make a just int array of that values and just get value using your int messagesCategory. And directly set it to TextView..

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

Sidebar

Related Questions

I have a question regarding the best practise of handling formated text when using
This question is related to database/RDBMS where i have little knowledge regarding performance and
i have a question regarding the AsyncTask class in android, and why it is
I have a question regarding applying a RTRIM on a ASP:Hyperlink statement. The code
So I have a question regarding the best practice for a PHP page inserting
I have a question regarding performance. Is there a performance penalty when I call
I have a question regarding to the performance between a lookup table stored in
I have a simple question regarding the dichotomy of joining two tables vs using
I have a question regarding the performance of an application. I have an object
I have question regarding the SQLAlchemy. How can I add into my mapped class

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.