Android App Development help: I need help getting my string to transfer between classes in android. Especially with the onCreate method in the way

StackOverflow https://stackoverflow.com/questions/22876234

Question

This is one class:

package com.feed.congressfeedback;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SendEmailActivity extends Activity {

Button buttonSend, buttonPrompts;
EditText textTo;
EditText textSubject;
EditText textMessage;
EditText name1;
String cc;




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


    buttonSend = (Button) findViewById(R.id.buttonSend);
    textTo = (EditText) findViewById(R.id.editTextTo);
    textSubject = (EditText) findViewById(R.id.editTextSubject);
    textMessage = (EditText) findViewById(R.id.editTextMessage);
    buttonPrompts = (Button) findViewById(R.id.prompts);
    name1 = (EditText) findViewById(R.id.name);
    buttonPrompts.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent in = new Intent("com.feed.congressfeedback.PromptsList");
            startActivity(in);
        }
    });

    buttonSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        String bm = "The Honorable Richard Shelby\n 304 Russell Senate Office Building\nUnited States Senate\nWashington, DC 20510\n\nDear Senator,\n";

          String to = textTo.getText().toString();
          String subject = textSubject.getText().toString();
          String namee = name1.getText().toString(); 
          String message =bm+"\t"+ textMessage.getText().toString(); 
          String finalmes = message +"\n\nSincerely, "+namee;

          Intent email = new Intent(Intent.ACTION_SEND);
          email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
          //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
          //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
          email.putExtra(Intent.EXTRA_SUBJECT, subject);
          email.putExtra(Intent.EXTRA_TEXT, finalmes);

          //need this to prompts email client only
          email.setType("message/rfc822");

          startActivity(Intent.createChooser(email, "Choose an Email client :"));

        }
    });
}

And this is the other:

package com.feed.congressfeedback;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class StatesList extends ListActivity {

String states[] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California","Colorado","Connecticut",           "Delaware", "Florida"
        ,"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas","Kentuky",      "Louisiana", "Maine", "Maryland", "Masssachusetts"
        ,"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana","Nebraska","Nevada","New Hamsphire", "New Jersey", "New Mexico"
        , "New York", "North Carolina", "North Dakota","Ohio", "Oklahoma", "Oregon", "Pennsylvania","Rhode Island","South Carolina", "South Dakota",
        "Tennessee", "Texas", "Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(StatesList.this, android.R.layout.simple_list_item_1, states));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String cheese = states[position];
    try{

        if (cheese == states[0]){

            Intent intent = new Intent("android.intent.action.THIRD");
            startActivity(intent);
        }
    }catch (Exception e){

    }
    }

}

What i am trying to do is: I want to when i click on a list item such as "Alabama", I want the EditText textTo to become something like alabama. When I click on "alaska" i want it so change EditText to "alaska". I dont know how to effectively share variables between classes on android. I would thike the code to be in this block:

 try{

        if (cheese == states[0]){

            Intent intent = new Intent("android.intent.action.THIRD");
            startActivity(intent);
        }
    }catch (Exception e){
Was it helpful?

Solution

You should put it in the intent. In your StatesList:

Intent intent = new Intent("android.intent.action.THIRD", SendEmailActivity.class);
intent.putExtra("to", cheese);
startActivity(intent);

And to get it back in your SendEmailActivity.onCreate():

String to = getIntent().getStringExtra("to");
textTo.setText(to);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top