简体   繁体   English

获取Asynctask中EditText的值

[英]obtaining the value of an EditText inside Asynctask

I am trying to make an app which uses FTP and changes the filename to a combination of 2 EditTexts. 我正在尝试制作一个使用FTP并将文件名更改为2 EditText组合的应用程序。 to properly upload it i am uploading it inside a 'asynctask' ,this is my code: 为了正确上传它,我正在'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;
    }
}

My problem is as follows: i want to use the values of week_text.getText().toString(); 我的问题如下:我想使用week_text.getText().toString();的值week_text.getText().toString(); and pagina_text.getText().toString(); pagina_text.getText().toString(); in my Asynctask, but i cant find a way to achieve this. 在我的Asynctask中,但我找不到实现此目的的方法。 i also have zero clue on what to do with the parameters behind Asynchtask, i have looked it up multiple times but it just doesnt make sense when using it for a FTP upload. 我也对与Asynchtask后面的参数有什么关系一无所知,我已经多次查询了它,但是将它用于FTP上传时却没有任何意义。

Help please ._. 请帮助 。_。

Just pass String values to execute method like below 只需传递String值即可执行如下所示的方法

new upload_task().execute(edtText1.getText.toString,edtText2.getText.toString);

then 然后

@Override
protected String doInBackground(String... params) {
    String editText1Value = params[0];
    String editText2Value = params[1];
   ///then do what ever you want
}

Just add the EditText` as parameter: 只需添加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:
    }
}

And call it: 并称之为:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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