无法将textView文本从MainActivity(第一个活动)传递到UserprofileActivity(第二个活动)

问题描述 投票:0回答:1

我的项目面临的问题,例如无法将textView值从firstActivity(MainActivity)传递给secondActivity(profileActivity)textview。

是我使用Backgroundworker(AsyncTask)的原因,所以无法直接传递数据吗?

Backgroundworker(AsyncTask)是用于mysql数据库和android studio目的交互的java类。

参考:AsyncTask References

如果intent方法不能与AsyncTask backgound类一起使用,那么我应该指代什么方法?

下面是MainActivity(又名登录活动),粗体是意图部分:

    import ....;

    public class MainActivity extends AppCompatActivity {
        Button _button;
        EditText usernamelogin, passwd;

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

            _button=findViewById(R.id.button);

            usernamelogin =findViewById(R.id.usernamelogin);
            passwd = findViewById(R.id.passwd);


        }

        public void OnLogin(View view){

                String user_name = usernamelogin.getText().toString();
                String pass_word = passwd.getText().toString();
                String type = "login";

               BackgroundTask backgroundTask = new BackgroundTask(this);
                backgroundTask.execute(type, user_name, pass_word);
                Intent intent = new Intent(MainActivity.this,ProfileActivity.class);
                intent.putExtra("userprofilename",_usernamelogin.getText().toString());   
            }  
        }
   }

Intent intent = new Intent(MainActivity.this,ProfileActivity.class);intent.putExtra(“ userprofilename”,_ usernamelogin.getText()。toString());

下面是SecondActivity:

import......;
public class ProfileActivity extends AppCompatActivity {
    TextView logouttxt;
    TextView profilename;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
    profilename=(TextView)findViewById(R.id.userproname);
        logouttxt =findViewById(R.id.logouttxt);
        profilename.setText(getIntent().getStringExtra("userprofilename"));



    }
}

profilename.setText(getIntent()。getStringExtra(“ userprofilename”));

下面是我的Backgroundworker(AsyncTask)类:

import .....;



public class BackgroundTask extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;

    BackgroundTask(Context ctx){
        context=ctx;
    }

    @Override
    protected String doInBackground(String...params) {


        String type = params[0];

        String loginURL ="http://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

        if(type.equals("login")) {

            try {
            String user_name = params[1];
            String pass_word = params[2];

                URL url = new URL(loginURL);

                    HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream outputStream = httpURLConnection.getOutputStream();
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                    String login_data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&"
                            + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(pass_word, "UTF-8");


                    bufferedWriter.write(login_data);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    outputStream.close();

                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));

                    String result="";
                    String line="";
                    while((line = bufferedReader.readLine())!= null) {
                        result += line;
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();

            }


                 catch (IOException e) {
                    e.printStackTrace();
                }


        }
            return null;
        }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);



        if(result.contentEquals("Login Success")) {
            Toast.makeText(context, "Login Successfully!", Toast.LENGTH_SHORT).show();
            context.startActivity(new Intent(context, ProfileActivity.class));



        }else
        {
            Toast.makeText(context, "Login Fail!", Toast.LENGTH_SHORT).show();
        }



    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
    }

谢谢。

java php android android-studio android-asynctask
1个回答
0
投票

如果您希望onPostExecute调用将用户名传递给下一个活动,则必须在其中添加用户名。

public class BackgroundTask extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;
    private String user_name = "";  // <-- STORE THE USER NAME

    BackgroundTask(Context ctx){
        context=ctx;
    }

    @Override
    protected String doInBackground(String...params) {

        String type = params[0];

        String loginURL ="http://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

        if(type.equals("login")) {

            try {
                user_name = params[1];
                String pass_word = params[2];

                // do stuff

                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();

            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }


    @Override
    protected void onPostExecute(String result) {
        if(result != null && result.contentEquals("Login Success")) {
            Toast.makeText(context, "Login Successfully!", Toast.LENGTH_SHORT).show();

            // ADD THE STORED USER NAME TO THE INTENT
            Intent i = new Intent(context, ProfileActivity.class);
            i.putExtra("userprofilename",user_name);
            context.startActivity(i);
        }
        else {
            Toast.makeText(context, "Login Fail!", Toast.LENGTH_SHORT).show();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.