【问题标题】:get EditText value in doInBackground AsyncTask在 doInBackground AsyncTask 中获取 EditText 值
【发布时间】:2016-04-21 07:42:14
【问题描述】:

我想在 doInBackground AsyncTask 中传递 Edittext 的值,我尝试了这段代码:

    protected void onCreate(Bundle savedInstanceState) {
    ID = (EditText) findViewById(R.id.ID);
    lname = (EditText) findViewById(R.id.lname);
    fname = (EditText) findViewById(R.id.fname);
    phone = (EditText) findViewById(R.id.phone);
    H = ID.getText().toString();
    Q = lname.getText().toString();
    C = fname.getText().toString();
    Ls = phone.getText().toString();

    addbtn = (Button) findViewById(R.id.addbtn);
    addbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
        new CreateNewexercise().execute(H,Q,C,Ls);
    }
    });
}

class CreateNewexercise extends AsyncTask<String, String, String> {
    /**
     * Creating exercise
     */
    protected String doInBackground(String... args) {
        //getting field's text
        id=args[0];
        ln=args[1];
        fn=args[2];
        p=args[3];

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ID", id));
        params.add(new BasicNameValuePair("lname", ln));
        params.add(new BasicNameValuePair("fname",fn));
        params.add(new BasicNameValuePair("phone", p));
        Log.d("ID", "Value: " + id);
        Log.d("lname", "Value: " + ln);
        Log.d("fname", "Value: " + fn);
        Log.d("phone", "Value: " + p);

我将参数作为参数传递,但它总是为空我不知道为什么?

谁能告诉我我做错了什么?

【问题讨论】:

    标签: android android-studio android-asynctask android-edittext


    【解决方案1】:

    您在onCreate 方法中从EditText 加载文本,而不是在单击按钮时按需加载。从OnClickListener 外部删除调用getText() 的行并将它们添加到内部。

    addbtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            H = ID.getText().toString();
            Q = lname.getText().toString();
            C = fname.getText().toString();
            Ls = phone.getText().toString();
            new CreateNewexercise().execute(H,Q,C,Ls);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 2021-09-21
      • 2012-11-05
      相关资源
      最近更新 更多