将数据传递给另一个 Java 类 Android Studio

Posted

技术标签:

【中文标题】将数据传递给另一个 Java 类 Android Studio【英文标题】:Passing Data to Another Java Class Android Studio 【发布时间】:2021-12-08 06:13:40 【问题描述】:

我想将数据从 LoginActivty 类传递给 CUpdateAccountActivity 类,但是当我转到 CUpdateAccountActivity 类时,该值丢失了,但是当我签入 LoginActivity 类时,它有该值,但不能将它传递给另一个上课,有人可以给点建议吗?我不知道发生了什么,它似乎无法传递数据,我从 CUpdateAccountActivity 类收到的都是 Null。

public class LoginActivity extends AppCompatActivity 

EditText userlogtxt, passlogtxt;
Button loginlogbtn;

DBHelper myDB;
CBuyFuelActivity cb;
CUpdateAccountActivity cp;
public String username1;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    userlogtxt = findViewById(R.id.userlogtxt);
    passlogtxt = findViewById(R.id.passlogtxt);
    loginlogbtn = findViewById(R.id.loginlogbtn);

    myDB = new DBHelper(this);
    cb = new CBuyFuelActivity();
    cp = new CUpdateAccountActivity();

    loginlogbtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            String username = userlogtxt.getText().toString();
            String password = passlogtxt.getText().toString();
            String type1 = "admin";
            String type2 = "driver";
            String type3 = "client";

            if(username.equals("") || password.equals("")) 
                Toast.makeText(LoginActivity.this, "Enter Credentials", Toast.LENGTH_SHORT).show();
            
            else 
                Boolean result = myDB.checkAccifAdmin(username,password);
                Boolean result2 = myDB.checkAccifDriver(username,password);
                Boolean result3 = myDB.checkAccifClient(username,password);

                if(result == true) 
                    Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
                    startActivity(intent);
                    Toast.makeText(LoginActivity.this, "Welcome Admin!", Toast.LENGTH_SHORT).show();
                    Toast.makeText(LoginActivity.this, "" + cb.cusername + cb.cpassword, Toast.LENGTH_SHORT).show();
                
                else if(result2 == true) 
                    Toast.makeText(LoginActivity.this, "Welcome Driver!", Toast.LENGTH_SHORT).show();
                
                else if(result3 == true) 
                    username1 = userlogtxt.getText().toString();
                    cp.name1 = username1;//I AM STARTING TO PASS THE DATA HERE
                    Intent intent = new Intent(getApplicationContext(), CHomeActivity.class);
                    startActivity(intent);
                    Toast.makeText(LoginActivity.this, "Welcome Client!" +cp.name1, Toast.LENGTH_SHORT).show();
                
                else 
                    Toast.makeText(LoginActivity.this, "Invalid Credentials", Toast.LENGTH_SHORT).show();
                
            
        
    );


public class CUpdateAccountActivity extends AppCompatActivity 

DBHelper myDB;
EditText cfirstnametxt, clastnametxt, cusernametxt, cpasswordtxt, ccontacttxt, cagetxt;
Button csavebtn;
LoginActivity lg;

public String name1; //I AM EXPECTING TO RECEIVE A DATA FROM LoginActiviy CLASS AND STORE IT IN A NAME1 STRING VARIABLE

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cupdate_account);
    lg = new LoginActivity();
    myDB = new DBHelper(this);
    //cp = new CUpdateAccountActivity();

    cfirstnametxt = findViewById(R.id.cfirstnametxt);
    clastnametxt = findViewById(R.id.clastnametxt);
    cusernametxt = findViewById(R.id.cusernametxt);
    cpasswordtxt = findViewById(R.id.cpasswordtxt);
    ccontacttxt = findViewById(R.id.ccontacttxt);
    cagetxt = findViewById(R.id.cagetxt);

    csavebtn = findViewById(R.id.csavebtn);

    viewAccData();

    csavebtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            Toast.makeText(CUpdateAccountActivity.this, ""+name1, Toast.LENGTH_SHORT).show();
        
    );


【问题讨论】:

but can't pass it to another class 你从哪里传过来的? lg = new LoginActivity() 这是一场灾难。当您创建 Intent 时,您不会创建 Activity Os 的对象。在进入应用程序之前,请阅读基本组件。 【参考方案1】:

您可以通过 Intent extras 将值传递给由 startActivity(the_intent) 启动的活动。

以下是您的代码的简化版本。初始活动是 LoginActivity。这只有一个按钮,当单击该按钮时,将使用从 LoginActivity 传递的两个值(一个 String 和一个 Long)启动 CUpdateAccountActivity。这些值从 Intent 中提取,然后 a) 显示在 TextView 中,b) 写入日志和 c) Toasted。

第一个LoginAactivity:-

public class LoginActivity extends AppCompatActivity 

    private Button loginBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginBtn = this.findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                Intent intent = new Intent(LoginActivity.this,CUpdateAccountActivity.class);
                /* use the intent extra keys as per the CUpdateAccountActivity class */
                /* passed values hard coded for brevity of demo */
                intent.putExtra(CUpdateAccountActivity.IE_USERNAMEKEY,"A String value to be passed");
                intent.putExtra(CUpdateAccountActivity.IE_USERID,100L);
                startActivity(intent);
            
        );
    

第二个CUpdateAccountActivity:-

public class CUpdateAccountActivity extends AppCompatActivity 

    /* IntentExtra Keys - public so can be accessed by other classes */
    public static final String IE_USERNAMEKEY = "ie_usernameKey";
    public static final String IE_USERID = "ie_userid";

    String passedUserName = "";
    long passedUserId;
    TextView passedValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cupdate_account);
        passedValues = this.findViewById(R.id.passedValues);
        /* Get the passed values */
        passedUserName = this.getIntent().getStringExtra(IE_USERNAMEKEY);
        passedUserId = this.getIntent().getLongExtra(IE_USERID,-1);

        /* Use the passed values
            a) in the TextView
            b) written to the log
            c) via Toast
        * */
        passedValues.setText("Login Activity passed > "
                + passedUserName + "< via extra " + IE_USERNAMEKEY
                + "< and >"
                + passedUserId + "< via extra " + IE_USERID
        );

        Log.d("PASSEDVALUE","Login Activity passed >" + passedUserName +"< via extra >" + IE_USERNAMEKEY);
        Log.d("PASSEDVALUE","Login Activity passed >  " + passedUserId + "< via extra >" + IE_USERID);

        Toast.makeText(
                this,
                "Loging Activity passed >" + passedUserName + "< via extra " + IE_USERNAMEKEY
                        + " and >" + passedUserId + "< via extra " + IE_USERID,
                Toast.LENGTH_SHORT
        ).show();
    

启动演示应用时显示:-

单击登录按钮会导致:-

toast 也会显示,日志包括:-

2021-10-22 15:55:55.509 D/PASSEDVALUE: Login Activity passed >A String value to be passed< via extra >ie_usernameKey
2021-10-22 15:55:55.509 D/PASSEDVALUE: Login Activity passed >  100< via extra >ie_userid

【讨论】:

以上是关于将数据传递给另一个 Java 类 Android Studio的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中不使用 ODBC.DataReader 将数据传递给另一个类

将数据传递给另一个组件[重复]

通过 Redirect 将数据传递给组件

将数据传递给服务 - android?

如何将数据传递给另一个视图函数进行处理

Vue-Router 将数据传递给另一个组件