Android: How to grab username info after login and display welcome 'username' in MainActivity
I have done alot of search for this and most of them are not for android.
I am using sharedpref to save the username in session until logout. I would like to display welcome 'username' in the mainactivity.
. For now I would like a sample code on grabbing the 'username' within the mainactivity class thats saved in sharedprefs and display it in textview.
Below is my login class that opens the mainActivity
开发者_JAVA百科 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", username);
editor.commit();
if(prefs.getString("username", null)!=null)
{Intent i = new Intent(getApplicationContext(), Customer.class);
startActivity(i);}
etUsername = (EditText)findViewById(R.id.username);
btnLogin = (Button)findViewById(R.id.login_button);
btnCancel = (Button)findViewById(R.id.cancel_button);
lblResult = (TextView)findViewById(R.id.result);
btnLogin.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
// Check Login
String username = etUsername.getText().toString();
if(username.equals("1111")){
lblResult.setText("Login successful.");
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
else if(username.equals("2222")){
lblResult.setText("Login successful.");
Intent i = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(i);
}
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close the application
finish();
}
}); }
MainActivity.java
public class MainActivity extends ListActivity
{
TextView selection;
CustomerListItem[] items = {
new CustomerListItem("Start Trip", StartTripActivity.class),
new CustomerListItem("Clock in", ClockinActivity.class),
new CustomerListItem("Log Out", LogoutActivity.class)};
private TextView resultsTxt;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.customer);
setListAdapter(new ArrayAdapter<CustomerListItem>(
this, android.R.layout.simple_list_item_1, items));
selection = (TextView) findViewById(R.id.selection);
showname = (TextView) findViewById(R.id.showname);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent(this, items[position].getActivity());
startActivityForResult(intent, position);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK)
{
// Perform different actions based on from which activity is
// the application returning:
switch (requestCode)
{
case 0:
// TODO: handle the return of the
break;
case 1:
// TODO: handle the return of the
break;
case 2:
// TODO: handle the return of the
break;
default:
break;
}
}
else if (resultCode == RESULT_CANCELED)
{
resultsTxt.setText("Canceled");
}
}
}
Try...
In your login Activity:
SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
prefs.edit().putString("username", username).commit();
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
In your main activity...
public class MainActivity extends Activity {
private String username = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(...) here
SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
username = prefs.getString("username", "UNKNOWN");
...
}
}
First, you should pass the username as an extra so the next activity can grab it. Put this in your login activity:
String username = prefs.getString("username");
Intent i = new Intent(this, MainActivity.class);
// this is where you should pass the username
i.putExtra("username", username);
startActivity(i);
After that, put this in your MainActivity, probably in the onCreate
method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainview);
Bundle extras = getIntent().getExtras();
if (extras.containsKey("username")) {
String username = extras.getString("username");
// put whatever code you want here to show the username
}
}
Hope it answers your question.
精彩评论