0

我按照这个网站做项目 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 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

4 回答 4

2

您正在创建两个意图。第一个包含捆绑包,但随后您立即创建一个没有捆绑包的意图来启动第二个活动,该活动覆盖第一个活动。如果您愿意,只需将标志添加到原始意图:

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

根本不需要第二个意图。

于 2013-02-20T03:17:50.983 回答
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");
于 2013-02-20T04:17:04.833 回答
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");
}
于 2013-02-20T04:33:13.297 回答
0

如果您使用“共享偏好”,而不是使用意图,它将更加简单和可靠

于 2013-02-20T04:49:51.757 回答