java—如何从第一个活动的文本视图中获取数据并从另一个活动中发布?

htrmnn0y  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(362)

我希望第二个活动中textview的数据在第三个活动中传递,但我不想在第三个活动中显示它。在最后一个活动中单击按钮后,我想直接发送第二个活动中的数据以传入我的sqldb。是否可以实现?这是我的第一个活动。

public class MainActivity extends AppCompatActivity {
private EditText email, password;
private Button btn_login;
private ProgressBar loading;
private static String URL_LOGIN ="http://192.168.1.83/Attendance/login.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loading= findViewById(R.id.loading);
    email = findViewById(R.id.editTextUserEmail);
    password = findViewById(R.id.editTextPassword);
    btn_login = findViewById(R.id.buttonRegister);

    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String mEmail = email.getText().toString().trim();
            String mPassword = password.getText().toString().trim();

            if (!mEmail.isEmpty() || !mPassword.isEmpty()) {
                Login(mEmail, mPassword);
            }else{
                email.setError("Please Enter Email");
                password.setError("Please Enter Password");

            }

        }
    });

}

private void Login(final String email, final String password) {
    loading.setVisibility(View.VISIBLE);
    btn_login.setVisibility(View.GONE);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        Log.d("JSON", jsonObject.toString());
                        String success = jsonObject.getString("success");
                        JSONArray jsonArray = 
jsonObject.getJSONArray("login");

                        if (success.equals("1"))
                        {
                            for (int i = 0; i<jsonArray.length(); i++){
                                JSONObject object = 
jsonArray.getJSONObject(i);

                                String name = 
object.getString("name").trim();
                                String email = 
object.getString("email").trim();

//                                    Toast.makeText(MainActivity.this, 
"Success Login \nYour name: "+name+"\nYour Email: "+email,
 //                                            Toast.LENGTH_LONG).show();
                                Intent intent = new 
  Intent(MainActivity.this, HomeActivity.class);
                                intent.putExtra("name",name);
                                intent.putExtra("email", email);
                                startActivity(intent);
                              loading.setVisibility(View.GONE);

                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        loading.setVisibility(View.GONE);
                        btn_login.setVisibility(View.VISIBLE);
                        Toast.makeText(MainActivity.this, "Error" 
 +e.toString(),
                                Toast.LENGTH_LONG).show();

                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, "Error" 
+error.toString(),
                            Toast.LENGTH_LONG).show();

                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("email", email);
            params.put("password", password);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

这是我的第二项活动

public class HomeActivity extends AppCompatActivity {
private TextView name,email;
private TextView welcome;
private Button Send;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    name = findViewById(R.id.name);
    email = findViewById(R.id.email);
    Send = findViewById(R.id.btn_send);
    welcome = findViewById(R.id.welcome);

    final Intent intent = getIntent();
    String extraName = intent.getStringExtra("name");
    String extraEmail = intent.getStringExtra("email");

    name.setText(extraName);
    email.setText(extraEmail);

    Send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent1 = new Intent(HomeActivity.this,StatusActivity.class);
            intent1.putExtra("wel", welcome.getText().toString());
            intent1.putExtra("name1", name.getText().toString());
            intent1.putExtra("email1", email.getText().toString());
            startActivity(intent1);

        }
    });
}

这是我的最后一个活动,但这里我不想显示文本视图,我只想从这里发布它。

public class StatusActivity extends AppCompatActivity {
private TextView welcome, name, email;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_status);
    welcome = findViewById(R.id.abcd);
    name = findViewById(R.id.name1);
    email = findViewById(R.id.email1);

    final Intent intent = getIntent();
    String extraName1 = intent.getStringExtra("name1");
    String extraEmail1 = intent.getStringExtra("email1");
    String extraWelcome = intent.getStringExtra("wel");

    name.setText(extraName1);
    email.setText(extraEmail1);
    welcome.setText(extraWelcome);

}
wi3ka0sx

wi3ka0sx1#

你的问题很清楚,直到你把mysql的主题。为什么要通过数据库将数据从一个活动传递到另一个活动。为什么需要花费时间和资源的数据库操作。
您可以通过将数据从一个活动传递到另一个活动 Intent s(基本值, String s和 Parcelable s) 哦!
你所要做的就是:
获取数据(从textview获取案例)。
创建导航到下一个活动的意图。
把数据放到目的地。
开始活动。
从意图中检索数据。
例如,如果将通过按钮单击导航到下一个活动,则在该按钮的 onClick() :

// 1.
String data = myTextView.getText();
// 2. 
Intent intent = new Intent(yourContext,YourNextActivity.class);
// 3.
intent.putStringExtra("MY DATA KEY",data);
// 4.
yourContext.startActivity(intent);
// 5. Now in YourNextActivity's onCreate();
String retrievedData = YourNextActivity.this.getIntent().getStringExtra("MY DATA KEY");

// Done!

相关问题