Martin TT
Martin TT

Reputation: 311

how to get a value to other activity

i follow this website to do the project http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

Now,i want to get the username, to other activity and display it. i have tried,but i cannot be successful. what should i do? that is my code

public class LoginActivity extends Activity {
Button btnLogin;
EditText inputusername;
EditText inputPassword;
TextView loginErrorMsg;

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";


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



    // Importing all assets like buttons, text fields
    inputusername = (EditText) findViewById(R.id.loginusername);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    loginErrorMsg = (TextView) findViewById(R.id.login_error);

    // Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {



        public void onClick(View view) {
            String username = inputusername.getText().toString();
            String password = inputPassword.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            Log.d("Button", "Login");
            JSONObject json = userFunction.loginUser(username, password);

            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    loginErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS); 
                    if(Integer.parseInt(res) == 1){
                        // user successfully logged in
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        // Clear all previous data in database
                        userFunction.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID));     


                        //get username
                        EditText et = (EditText)findViewById(R.id.loginusername);
                        String name = et.getText().toString();

                        //new Intent对
                        Intent intent = new Intent();
                        intent.setClass(LoginActivity.this, DashboardActivity.class);

                        Bundle bundle = new Bundle();
                        bundle.putString("name",name);

                        intent.putExtras(bundle);

                        startActivity(intent);

                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                        // Close all views before launching Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);

                        // Close Login Screen
                        finish();
                    }else{
                        // Error in login
                        loginErrorMsg.setText("Incorrect username/password");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });


}
}

public class DashboardActivity extends Activity {
UserFunctions userFunctions;
Button btnLogout;
Button btnViewOrders;
Button btnViewProfile;

private EditText hihi;

String pid;

private static String KEY_NAME = "name";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //get Intent Bundle
    Bundle bundle = this.getIntent().getExtras();       

    String name = bundle.getString("name");     

    TextView mytv = (TextView)findViewById(R.id.abc);
    mytv.setText("you name is:" + name); 


    /**
     * Dashboard Screen for the application
     * */
    // Check login status in database
    userFunctions = new UserFunctions();
    if(userFunctions.isUserLoggedIn(getApplicationContext())){
   // user already logged in show databoard
        setContentView(R.layout.dashboard);
        btnLogout = (Button) findViewById(R.id.btnLogout);

        btnLogout.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                userFunctions.logoutUser(getApplicationContext());
                Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                // Closing dashboard screen
                finish();
            }
        });




    }else{
        // user is not logged in show login screen
        Intent login = new Intent(getApplicationContext(), LoginActivity.class);
        login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(login);
        // Closing dashboard screen
        finish();
    }
}    

}

Upvotes: 0

Views: 846

Answers (4)

Narendra
Narendra

Reputation: 609

Instead of using intents it will be much simple and reliable if you use "shared preferences"

Upvotes: 0

Zankhna
Zankhna

Reputation: 4563

Try following code :

To pass value from LoginActivity :

Intent intent = new Intent(getApplicationContext(),DashboardActivity.class);
intent.putExtra("name",name);
startActivity(intent);

To get value in DashboardActivity:

Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString("name");
}

Upvotes: 0

duggu
duggu

Reputation: 38419

try the following :-

pass value from one activity (Activity1.java)

       Intent myIntent = new Intent(Activity1.this, Activity2.class);
       myIntent.putExtra("name","name");
       myIntent.putExtra("id","65");
       startActivity(myIntent); 

get value from another activity  (Activity2.java)

    Intent intent = getIntent();
    String name=intent.getStringExtra("name");
    String id=intent.getStringExtra("id");

Upvotes: 0

Geobits
Geobits

Reputation: 22342

You're creating two intents. The first holds the bundle, but then you immediately create an intent with no bundle to start a second activity that overrides the first. You can just add the flag to the original intent if you want to:

//new Intent对
Intent intent = new Intent();
intent.setClass(LoginActivity.this, DashboardActivity.class);

Bundle bundle = new Bundle();
bundle.putString("name",name);

intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

There's no need for a second intent at all.

Upvotes: 2

Related Questions