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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:07:57+00:00 2026-05-26T15:07:57+00:00

I have my main application class as follows and what I would like to

  • 0

I have my main application class as follows and what I would like to know is how to change one line to call a method either from same class or another class, whilst the others still call activities. Here is the code:

public class InfoActivity extends GDListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.info_activity_title);

    ItemAdapter adapter = new ItemAdapter(this);
    adapter.add(createTextItem(R.string.info_about, AboutActivity.class));
    adapter.add(createTextItem(R.string.info_terms, TermsActivity.class));
    adapter.add(createTextItem(R.string.info_privacy, PrivacyActivity.class));

    setListAdapter(adapter);
}

private TextItem createTextItem(int stringId, Class<?> klass) {
    final TextItem textItem = new TextItem(getString(stringId));
    textItem.setTag(klass);
    return textItem;
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final TextItem textItem = (TextItem) l.getAdapter().getItem(position);
    Intent intent = new Intent(InfoActivity.this, (Class<?>) textItem.getTag());
    startActivity(intent);
}
}

The line in question:

adapter.add(createTextItem(R.string.info_about, AboutActivity.class));

I would like to call a method which as an example does this:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { AboutActivity.this.getString(R.string.feedback_email) } );
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, AboutActivity.this.getString(R.string.feedback_subject)); 
    emailIntent.setType("plain/text"); 
    startActivity(emailIntent);

Right now I have this in the onCreate method of AboutActivity, but there naturally is no reason to have this functionality (sending email) within an activity. Instead it can just be ran as is. So, how could I do this?

Thanks!

The other two lines:

 adapter.add(createTextItem(R.string.info_terms, TermsActivity.class));
    adapter.add(createTextItem(R.string.info_privacy, PrivacyActivity.class));

they can remain the same in terms of functionality. This question is an addendum from this one (which I asked earlier and got answered):

Android – call method instead of class – sdk

  • 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-26T15:07:57+00:00Added an answer on May 26, 2026 at 3:07 pm

    How ever you choose to go about this, the trick is to actually move the decision making about what to do when a particular item is clicked down into the onListItemClick method. Here’s a simple approach. First, change your createTextItem method to this:

    private TextItem createTextItem(int stringId) {
        final TextItem textItem = new TextItem(getString(stringId));
        return textItem;
    }
    

    Then, change your onListItemClick to this:

    protected void onListItemClick(ListView l, View v, int position, long id) {
        final TextItem textItem = (TextItem) l.getAdapter().getItem(position);
        String textItemContents = textItem.getString(); //I don't know if this is actually correct. I don't know what the TextItem class is. But I think you get the idea.
        Intent intent = getIntentForString(textItemContents);
        startActivity(Intent);
    }
    

    Your getIntentForString() method would then look something like this (note we can’t use a switch because support for using switch statements with strings has only just recently been added to java):

    private Intent getIntentForString(String textViewContents){
      if(textViewContents.equals(getString(R.string.info_about))){
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { 
          AboutActivity.this.getString(R.string.feedback_email) } );
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
          AboutActivity.this.getString(R.string.feedback_subject)); 
        emailIntent.setType("plain/text"); 
        return emailIntent;
      }
      else if(textViewContents.equals(getString(R.string.info_terms)){
        return new Intent(InfoActivity.this, TermsActivity.class);
      }
      else if(textViewContents.equals(getString(R.string.info_privacy)){
        return new Intent(InfoActivity.this, Privacy.class);
      }
      else{
        return null;
      }
    }
    

    Note this approach has a downfall though. If yous start adding a bunch of different items to your ListView you’re going to need to grow and grow your getIntentForString() method. For right now though, this should suffice. If you find yourself adding more options to your ListView though they’ll be a more complicated approach that we’ll need to take.

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

Sidebar

Related Questions

I have a java application where main class is dependent with some other classes.
I have my main application delegate which contains a method that returns an object.
I have my main application ,from my main application I will be calling another
I have WPF Application where I have One main form and other user controls
I have an application structured as follows: dao domain main services utils I've made
In a game application I have the following scenario: From the main game Activity
I found one sample application from the Blackberry knowledgebase. From that application I have
I have a spring application and I make my test class as follows: @RunWith(SpringJUnit4ClassRunner.class)
I have a main application, and a bunch of sub-applications (they are separate apps,
I have a java main application, and I want to use log4j. I don't

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.