在 Asynctask 中获取 EditText 的值
obtaining the value of an EditText inside Asynctask
我正在尝试制作一个应用程序,它使用 FTP 并将文件名更改为 2 个 EditText 的组合。为了正确上传它,我将它上传到 'asynctask' 中,这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
EditText week_text = (EditText) findViewById(R.id.week_edit);
EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
String week = "w" + week_text.getText().toString() + "_";
String pagina = "p" + pagina_text.getText().toString() + ".jpg";
Button foto_keuze = (Button)findViewById(R.id.foto_keuze_button);
Button upload_button = (Button)findViewById(R.id.upload_button);
Typeface Impact = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");
foto_keuze.setTypeface(Impact);
upload_button.setTypeface(Impact);
targetImage = (ImageView)findViewById(R.id.imageView);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
public void upload_klik (View view) {
EditText week_text = (EditText) findViewById(R.id.week_edit);
EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
upload_task.execute(week_text, pagina_text);
}
protected class upload_task extends AsyncTask<EditText, Object, String> {
@Override
protected String doInBackground(EditText... params) {
EditText w = params[0];
EditText p = params[1];
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String ret = "Done!";
if(!bundle.isEmpty()) {
String afdeling_url = bundle.getString("afdeling_url", "DKW/");
String afdeling_preFix = bundle.getString("afdeling_preFix", "dkw");
String locatie_url = bundle.getString("locatie_url", "delf_wend");
String new_fileName = afdeling_preFix + w + p;
File f = new File(foto_path);
File sdcard = Environment.getExternalStorageDirectory();
File to = new File(sdcard, new_fileName);
f.renameTo(to);
if(f == null){
Toast.makeText(upload.this, "Geen foto geselecteerd", Toast.LENGTH_SHORT).show();
}
if(f != null) {
try{
Toast.makeText(getApplicationContext(), afdeling_url + afdeling_preFix, Toast.LENGTH_SHORT).show();
client.setPassive(true);
client.setAutoNoopTimeout(30000);
client.connect(FTP_HOST, 21);
client.login(FTP_USER, FTP_PASS);
client.setType(FTPClient.TYPE_BINARY);
client.changeDirectory(locatie_url + afdeling_url);
client.upload(to, new FTP_LISTENER());
restart();
}
catch (Exception e){
e.printStackTrace();
try {
client.disconnect(true);
Toast.makeText(getApplicationContext(), "Upload voltooid", Toast.LENGTH_SHORT);
}
catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
return ret;
}
}
我的问题如下:我想在我的 Asynctask 中使用 week_text.getText().toString();
和 pagina_text.getText().toString();
的值,但我找不到实现此目的的方法。
我也对如何处理 Asynchtask 背后的参数一无所知,我已经多次查找它但是在将它用于 FTP 上传时它只是没有意义。
求助._.
只需添加 EditText` 作为参数:
protected class upload_task extends AsyncTask<EditText, Object, String> {
@Override
protected String doInBackground(EditText... params) {
EditText editText1 = params[0];
EditText editText2 = params[1];
///rest of code:
}
}
并称它为:
EditText week_text = (EditText) findViewById(R.id.week_edit);
EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
new upload_task().execute(week_text, paging_text);
只需将字符串值传递给执行方法,如下所示
new upload_task().execute(edtText1.getText.toString,edtText2.getText.toString);
然后
@Override
protected String doInBackground(String... params) {
String editText1Value = params[0];
String editText2Value = params[1];
///then do what ever you want
}
我正在尝试制作一个应用程序,它使用 FTP 并将文件名更改为 2 个 EditText 的组合。为了正确上传它,我将它上传到 'asynctask' 中,这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
EditText week_text = (EditText) findViewById(R.id.week_edit);
EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
String week = "w" + week_text.getText().toString() + "_";
String pagina = "p" + pagina_text.getText().toString() + ".jpg";
Button foto_keuze = (Button)findViewById(R.id.foto_keuze_button);
Button upload_button = (Button)findViewById(R.id.upload_button);
Typeface Impact = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");
foto_keuze.setTypeface(Impact);
upload_button.setTypeface(Impact);
targetImage = (ImageView)findViewById(R.id.imageView);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
public void upload_klik (View view) {
EditText week_text = (EditText) findViewById(R.id.week_edit);
EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
upload_task.execute(week_text, pagina_text);
}
protected class upload_task extends AsyncTask<EditText, Object, String> {
@Override
protected String doInBackground(EditText... params) {
EditText w = params[0];
EditText p = params[1];
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String ret = "Done!";
if(!bundle.isEmpty()) {
String afdeling_url = bundle.getString("afdeling_url", "DKW/");
String afdeling_preFix = bundle.getString("afdeling_preFix", "dkw");
String locatie_url = bundle.getString("locatie_url", "delf_wend");
String new_fileName = afdeling_preFix + w + p;
File f = new File(foto_path);
File sdcard = Environment.getExternalStorageDirectory();
File to = new File(sdcard, new_fileName);
f.renameTo(to);
if(f == null){
Toast.makeText(upload.this, "Geen foto geselecteerd", Toast.LENGTH_SHORT).show();
}
if(f != null) {
try{
Toast.makeText(getApplicationContext(), afdeling_url + afdeling_preFix, Toast.LENGTH_SHORT).show();
client.setPassive(true);
client.setAutoNoopTimeout(30000);
client.connect(FTP_HOST, 21);
client.login(FTP_USER, FTP_PASS);
client.setType(FTPClient.TYPE_BINARY);
client.changeDirectory(locatie_url + afdeling_url);
client.upload(to, new FTP_LISTENER());
restart();
}
catch (Exception e){
e.printStackTrace();
try {
client.disconnect(true);
Toast.makeText(getApplicationContext(), "Upload voltooid", Toast.LENGTH_SHORT);
}
catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
return ret;
}
}
我的问题如下:我想在我的 Asynctask 中使用 week_text.getText().toString();
和 pagina_text.getText().toString();
的值,但我找不到实现此目的的方法。
我也对如何处理 Asynchtask 背后的参数一无所知,我已经多次查找它但是在将它用于 FTP 上传时它只是没有意义。
求助._.
只需添加 EditText` 作为参数:
protected class upload_task extends AsyncTask<EditText, Object, String> {
@Override
protected String doInBackground(EditText... params) {
EditText editText1 = params[0];
EditText editText2 = params[1];
///rest of code:
}
}
并称它为:
EditText week_text = (EditText) findViewById(R.id.week_edit);
EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
new upload_task().execute(week_text, paging_text);
只需将字符串值传递给执行方法,如下所示
new upload_task().execute(edtText1.getText.toString,edtText2.getText.toString);
然后
@Override
protected String doInBackground(String... params) {
String editText1Value = params[0];
String editText2Value = params[1];
///then do what ever you want
}