如何获取另一个活动的价值

时间:2013-02-20 03:06:18

标签: android

我按照这个网站来做这个项目 http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

现在,我想获取用户名,其他活动并显示它。 我试过了,但我不能成功。 我该怎么办? 那是我的代码

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 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();
    }
}    

}

4 个答案:

答案 0 :(得分:2)

你正在创造两个意图。第一个包含bundle,但是你立即创建一个没有bundle的intent来启动第二个覆盖第一个的活动。如果您愿意,可以将标志添加到原始意图中:

//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);

根本不需要第二个意图。

答案 1 :(得分:0)

尝试以下方法: -

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");

答案 2 :(得分:0)

请尝试以下代码:

从LoginActivity传递值:

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

要在DashboardActivity中获取价值:

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

答案 3 :(得分:0)

如果您使用“共享首选项”

,而不是使用意图,它将非常简单和可靠