简体   繁体   中英

Passing String to another Activity not possible

I have tried a lot of times and in different ways to pass a string from one activity (LoginActivity) to another one (SettingsActivity). Of course I did a lot of research and tried each provided solution, but nothing could help me until now

LoginActivity.class:

package com.naderk.pds;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;
import com.naderk.pds.contexts.ContextServerActivity;

import java.util.HashMap;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    final String LOG = "LoginActivity";

    Button btnLogin, btnRegister;
    EditText etUsername, etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);

        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnRegister = (Button) findViewById(R.id.bRegister);

        btnLogin.setOnClickListener(this);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent seeTasksIntent= new Intent(LoginActivity.this, RegisterActivity.class);
                LoginActivity.this.startActivity(seeTasksIntent);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        HashMap postData = new HashMap();

        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();

        postData.put("txtUsername", username);

        postData.put("txtPassword", password);

        PostResponseAsyncTask task1 = new PostResponseAsyncTask(LoginActivity.this, postData, new AsyncResponse() {
            @Override
            public void processFinish(String s) {
                Log.d(LOG, s);
                if(s.contains("success")){
                    Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "Wrong username or password. Please register and try again.", Toast.LENGTH_LONG).show();
                }
            }
        });
        task1.execute("http://192.168.0.11/customer/");

        visitactivity1();

    }

    public void visitactivity1() {
        Intent i = new Intent(LoginActivity.this, SettingsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("key", etUsername.getText().toString());
        i.putExtras(bundle);
        startActivity(i);
        finish();
    }
}

SettingsActivity.class:

package com.naderk.pds;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Intent;

public class SettingsActivity extends AppCompatActivity {


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

        TextView textView = (TextView) findViewById(R.id.tvUserName);

        Bundle bundle = getIntent().getExtras();

        String stuff = bundle.getString("key");

        textView.setText(stuff);

    }
}

Logcat:

Exceeds 30.000 characters lol

I really hope that someone is able to help me.

Cheers!

Try this

public void visitactivity1() {
        Intent i = new Intent(LoginActivity.this, SettingsActivity.class);
        i.putExtra("key", etUsername.getText().toString());
        startActivity(i);
        finish();
    }

and on the second activity

public class SettingsActivity extends AppCompatActivity {


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

            TextView textView = (TextView) findViewById(R.id.tvUserName);


            String stuff = getIntent().getStringExtra("key");

            textView.setText(stuff);

        }
    }

Hope it helps (:

You can add the putExtras

 if(s.contains("success")){
                Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.putExtra("passwordText", password.getText().toString());
                intent.putExtra("usernameText",username.getText().toString());
                startActivity(intent);

On your second activity implement the following within your onCreate() method

Intent intent = getIntent();
    String password = intent.getStringExtra("passwordText");
     String username = intent.getSringExtra("usernameText");

You can read this blog for more information about intents : What are intents In summary is a tutorial about how to implement intents. It may help you in case you want to change your structure.

Finally, I´m not sure if this is the way you wanted to pass the data : instead use it as I recommend. I hope it helps !

 postData.put("txtUsername", username);
 postData.put("txtPassword", password);

您可以使用 Bundle 方法在您的 Activity 类中发送和接收字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM