Reputation: 13
Im completely new to Android so apologies if this is a silly question. But my problem is this:
I have created a number of classes for each of the pages in my app and I would like to update the text in a textview of a particular class from the text in an edittext field from another class.
To be more specific, I have a login page and I want the username (input by the user in an edittext box) to be transferred to a textfield in the logged in page. Currently I am trying to achieve this by using a click listener for the log in button in the log in page:
public void sign_in_click(View view) {
EditText tv1 = (EditText) findViewById(R.id.txt_input_uname);
String username=tv1.getText().toString();
LoginDetails unamede=new LoginDetails();
unamede.setuname(username);
Intent myIntent = new Intent(view.getContext(), customer.class);
startActivityForResult(myIntent, 0);
}
So the click listener initialises a new class variable I have defined in another class like so:
public class LoginDetails {
public String uname;
public void setuname(String username){
uname=username;
}
public String getuname(){
return uname;
}
}
and it gives uname the username from the edittext box in the login page.
Then I have in the logged in page under oncreate:
LoginDetails unamed= LoginDetails();
String username=unamed.getuname();
tv1.setText(username);
but the text in the textview box doesnt get anything written to it. Now I wont be surprised if I'm doing this completely wrong but any advice would be much appreciated. thanks
Upvotes: 1
Views: 2179
Reputation: 8787
You could try putting the username to activity intent extra (when you don't want to persist it in the shared preferenced right now):
Intent myIntent = new Intent(view.getContext(), customer.class);
myIntent.putExtra("uname", username);
startActivityForResult(myIntent, 0);
And then in onCreate:
tv1.setText(getIntent().getStringExtra("uname"));
Upvotes: 0
Reputation: 26971
What i would suggest is put the user's log in information into a SharedPreference. To transfer the date to another activity.
For example...
SharedPreferences myPrefs = getSharedPreferences("USER_INFO", 0);
myPrefsEdit.putString("USER_ID", username=tv1.getText().toString(););
//This is the username you get from edittext
myPrefsEdit.putString("PASSWORD", password);
//this is the user password you get from edittext
myPrefsEdit.commit();
In your next activity.
Get reference to your SharePreference like...
SharedPreferences info = getSharedPreferences("USER_INFO, 0);
String username = info.getString("USER_ID", "DOESNT EXIST");
String userpassword = info.getString("PASSWORD", "DOESNT EXIST");
This should do it
Upvotes: 1