secondActivity (profileActivity) 텍스트 뷰에있게 전달하는 firstActivity (MainActivity)에서 텍스트 뷰 값되지 같은 내 프로젝트 얼굴 문제.
난 BackgroundWorker에 (AsyncTask를) 때문에, 데이터를 전달 지시 수를 사용하는 이유는 무엇입니까?
BackgroundWorker에 (AsyncTask를)은 MySQL의 데이터베이스와 안드로이드 스튜디오 목적 사이의 상호 작용에 대한 자바 클래스입니다.
참조 : AsyncTask를 참조
의도 방법은 AsyncTask를 backgound에 클래스와 함께 사용할 수없는 경우, 그래서 어떤 방법을 참조해야하는?
아래 MainActivity (AKA 로그인 활동)이며 굵은 텍스트는 의도 일부입니다
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());
}
}
}
의도 의도 = 새로운 의도 (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);
}
}
감사.
당신이 원하는 경우 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]; // <--- SET THE MEMBER VARIABLE
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();
}
}
}