Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Matheus G Oliveira
9,682 PointsStore strings of EditText and use in another class
Hello people,
i need to store names in a string added on edittext and use it in another activity.
Let me illustrate.
I have this activity
public class UserlogActivity extends ActionBarActivity implements TextWatcher {
EditText mNomeField1, mNomeField2, mNomeField3, mNomeField4, mNomeField5,
mNomeField6;
Button mGoButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userlog);
mNomeField1 = (EditText) findViewById(R.id.nomeField1);
mNomeField2 = (EditText) findViewById(R.id.nomeField2);
mNomeField3 = (EditText) findViewById(R.id.nomeField3);
mNomeField4 = (EditText) findViewById(R.id.nomeField4);
mNomeField5 = (EditText) findViewById(R.id.nomeField5);
mNomeField6 = (EditText) findViewById(R.id.nomeField6);
mGoButton = (Button) findViewById(R.id.buttonUserAdd);
mGoButton.setOnClickListener(new btnHandler2());
}
class btnHandler2 implements OnClickListener{
@Override
public void onClick(View v){
Intent intent = new Intent ( UserlogActivity.this, RandomPlayers.class );
String playerOne = mNomeField1.getText().toString();
String playerTwo = mNomeField2.getText().toString();
String playerThree = mNomeField3.getText().toString();
String playerFour = mNomeField4.getText().toString();
String playerFive = mNomeField5.getText().toString();
String playerSix = mNomeField6.getText().toString();
startActivity(intent);
}
}
@Override
public void afterTextChanged(Editable s) {
mGoButton.setEnabled(mNomeField1.length() > 0);
mGoButton.setEnabled(mNomeField2.length() > 0);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
I need to get the strings on all edittexts(if filled), and send it to another activity to be inserted on the rdmPlayers String
public class RandomPlayers extends UserlogActivity {
// member variables (properties about the object)
public static String[] rdmPlayers = {
"mNomeField1",
"mNomeField2",
"mNomeField3",
"mNomeField4",
"mNomeField5",
"mNomeField6",
};
Appreciate the help!!
5 Answers
cbcbcb
16,560 PointsHello Matheus G Oliveira. I've written down some code you can add that should help you accomplish what you are trying
1.) Declare and initialize a private static final String variable for each player at before the onCreate( ) method's signature:
public class UserlogActivity extends ActionBarActivity implements TextWatcher
{
public static final String[ ] PLAYERS =
{
"playerOne", "playerTwo", "playerThree", "playerFour", "playerFive", "playerSix";
}
2.) After your Intent object's constructor, delete the following block of code:
String playerOne = mNomeField1.getText().toString();
String playerTwo = mNomeField2.getText().toString();
String playerThree = mNomeField3.getText().toString();
String playerFour = mNomeField4.getText().toString();
String playerFive = mNomeField5.getText().toString();
String playerSix = mNomeField6.getText().toString();
3.) Right after your Intent object's constructor, add the following code ( The for loop is optional..implement it differently if preferred ):
EditText[ ] nomes = new EditText[ 5 ];
nomes[ 0 ] = mNomeField1;
nomes[ 1 ] = mNomeField2;
nomes[ 2 ] = mNomeField3;
nomes[ 3 ] = mNomeField4;
nomes[ 4 ] = mNomeField5;
nomes[ 5 ] = mNomeField6;
for( int i = 0; i < PLAYERS.length; i++ )
{
if( nomes[ i ].getText == null )
{
nomes[ i ].setText( "" );
}
intent.putExtra( PLAYERS[ i ].toString( ), nomes[ i ].getText( ).toString( ) );
}
4.) Access the Bundle object given as the parameter to the in RandomPlayers's onCreate( ) method after the setContentView( ); method and use a for loop to cycle through the data or value for each player which are each represented as a key-value pair:
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.random_players_layout );
Bundle players = getIntent().getExtras();
String[] playersData = new String[ UserLogActivity.PLAYERS.length ];
if (extras != null)
{
for( int i = 0; i < playersData.length; i++ )
{
playersData[ i ] = extras.getString( UserLogActivity.PLAYERS[ i ].toString( ) );
}
}
I did not change the way you declared and initialized your EditText variables but I do recommend applying the same technique shown for the first step to the EditText member variables as well by declaring them in an array of EditText variables and then in doing so you will need to reflect this by changing the reference variable(s) you used to initialize them to their respective value associated with the layout. I did not do this however in the examples shown below.
cbcbcb
16,560 PointsOops. Add the getText( ) methods back to the EditText variables. My mistake.
EditText[ ] nomes = new EditText[ 5 ];
nomes[ 0 ] = mNomeField1.getText( ).toString( );
nomes[ 1 ] = mNomeField2.getText( ).toString( );
nomes[ 2 ] = mNomeField3.getText( ).toString( );
nomes[ 3 ] = mNomeField4.getText( ).toString( );
nomes[ 4 ] = mNomeField5.getText( ).toString( );
nomes[ 5 ] = mNomeField6.getText( ).toString( );
Matheus G Oliveira
9,682 PointsStill returning errors, It says Type mismatch, cannot convert from String to EditText
extras cannot be resolved to a variable nomes cannot be resolved to a variable
cbcbcb
16,560 PointsYour design doesn't make sense. The RandomPlayers class is a subclass of a subclass of the Activity class. You will need to close this question as answered because your question was about passing data between two subclasses of the Activity class.
Matheus G Oliveira
9,682 PointsI can imagine it is not so good, i am a newbie on this. So i think i will correct it by the time Thank you very muchhhh!!
cbcbcb
16,560 PointsOpen another question for how to pass data to a background service from an Activity class and then how to retrieve data from a service class in an Activity class. Really what you need to look at is some sort of persistent data. Perhaps writing the values to a file and then reading those values from the file when you need to retrieve them.
Matheus G Oliveira
9,682 Pointsokay, i will search here if i find something, now that i know the exact name of what i need to look for Thanks again Christopher!
Matheus G Oliveira
9,682 PointsHey Christopher, thank you very much for answering but as i am a novice, I have some questions (a lot of them), i hope its okay for you.. hahahha
The RandomPlayers.class is just for storing the values of the edittext. I will call this class from another activity which has a layout. to be more specific, i am adding the names added on the edittext to a textview on an activity called ActivityAnswer.
I added your modifications but still i am having problems with a value called "extras".Eclipse says it cannot be resolved. check my code to see if its right
Here the UserlogActivity.class (has layout)
public class UserlogActivity extends ActionBarActivity implements TextWatcher {
EditText mNomeField1, mNomeField2, mNomeField3, mNomeField4, mNomeField5,
mNomeField6;
Button mGoButton;
public static final String[ ] PLAYERS =
{"playerOne", "playerTwo", "playerThree", "playerFour", "playerFive", "playerSix",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userlog);
mNomeField1 = (EditText) findViewById(R.id.nomeField1);
mNomeField2 = (EditText) findViewById(R.id.nomeField2);
mNomeField3 = (EditText) findViewById(R.id.nomeField3);
mNomeField4 = (EditText) findViewById(R.id.nomeField4);
mNomeField5 = (EditText) findViewById(R.id.nomeField5);
mNomeField6 = (EditText) findViewById(R.id.nomeField6);
mGoButton = (Button) findViewById(R.id.buttonUserAdd);
mGoButton.setOnClickListener(new btnHandler2());
}
class btnHandler2 implements OnClickListener{
@Override
public void onClick(View v){
Intent intent = new Intent ( UserlogActivity.this, RandomPlayers.class );
startActivity(intent);
EditText[ ] nomes = new EditText[ 5 ];
nomes[ 0 ] = mNomeField1;
nomes[ 1 ] = mNomeField2;
nomes[ 2 ] = mNomeField3;
nomes[ 3 ] = mNomeField4;
nomes[ 4 ] = mNomeField5;
nomes[ 5 ] = mNomeField6;
for( int i = 0; i < PLAYERS.length; i++ )
{
if( nomes[ i ].getText() == null )
{
nomes[ i ].setText( "" );
}
intent.putExtra( PLAYERS[ i ].toString( ), nomes[ i ].getText( ).toString( ) );
}