【问题标题】:How can i pass the EditText values to AsynkTask如何将 EditText 值传递给 AsyncTask
【发布时间】:2015-06-16 11:38:15
【问题描述】:

我正在我的应用程序上建立一个注册区域,一切正常。

我使用 PHP 插入,Java 获取值,因此,我想将 EditTexts 中的值传递给 AsynkTask 类并将它们提交到我的数据库。

我该怎么做?

public class register extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        final EditText e_id=(EditText) findViewById(R.id.editText1);
        final EditText e_name=(EditText) findViewById(R.id.editText2);
        Button clickButton = (Button) findViewById(R.id.button1);
        clickButton.setOnClickListener( new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        insert_user mTask = new insert_user();
                        mTask.execute("I WOULD LIKE TO PUT THE EDITTEXT VALUE HERE");

                        // TODO Auto-generated method stub
                    }
                });
    }


    class insert_user extends AsyncTask<String, Integer, String> {

        // Runs in UI before background thread is called
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            // Do something like display a progress bar
        }

        // This is run in a background thread
        @Override
        protected String doInBackground(String... params) {
            String passed = params[0];
            ArrayList<NameValuePair> nameValuePairs = new 

            ArrayList<NameValuePair>();


            nameValuePairs.add(new BasicNameValuePair("id","1"));
            nameValuePairs.add(new BasicNameValuePair("name",passed));

            InputStream is = null;
            try
            {
                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("http://10.0.2.2/example/insert.php");
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                 HttpResponse response = httpclient.execute(httppost); 
                 HttpEntity entity = response.getEntity();
                 is = entity.getContent();
                 Log.e("pass 1", "connection success ");
             }
             catch(Exception e)
             {
                 Log.e("Fail 1", e.toString());

             }     

             String result = null;
            try
            {
                 BufferedReader reader = new BufferedReader
            (new InputStreamReader(is,"iso-8859-1"),8);
                 StringBuilder sb = new StringBuilder();
                 String line;
                while ((line = reader.readLine()) != null)
                 {
                     sb.append(line + "\n");
                 }
                 is.close();
                 result = sb.toString();
                 Log.e("pass 2", "connection success ");
             }
             catch(Exception e)
             {
                  Log.e("Fail 2", e.toString());
             }     

             try
             {
                  JSONObject json_data = new JSONObject(result);
                  String id = json_data.getString("id");
                  String name =json_data.getString("name");
             }
             catch(Exception e)
             {
                 Log.e("Fail 3", e.toString());
             }
            return result;

    }
        // This runs in UI when background thread finishes
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            // Do things like hide the progress bar or change a TextView

        }
    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以覆盖构造函数。比如:

    class insert_user extends AsyncTask<String, Integer, String> {
    
        String mText;
    
        public insert_user(String text) {
            super();
            mText = text;       // save the value
        }
    
        protected String doInBackground(String... params) {
            // do something with mText
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    
    
            nameValuePairs.add(new BasicNameValuePair("id","1"));
            nameValuePairs.add(new BasicNameValuePair("name",mText));
    

    。 . .

        }
    
    .
    .
    .
    }
    

    然后实例化并运行

     insert_user mTask = new insert_user("EditText Value");
     mTask.execute();
    

    读取 e_name 值:

    insert_user mTask = new insert_user(e_name.getText().toString());
    mTask.execute();
    

    这将为 AsyncTask 的生命周期设置值,包括预执行。

    如果你只需要doInBackground中的String值,那么你可以直接传递一个参数数组,好文章Android: How can I pass parameters to AsyncTask's onPreExecute()?

    【讨论】:

    • nameValuePairs.add(new BasicNameValuePair("name",mText));
    • 我需要在哪个地方上课?
    • 不明白这个问题,您的代码显示 AsyncTask 在 onClick 处理程序中运行。编辑了我的答案,不确定是否有帮助。
    【解决方案2】:

    您传递编辑文本信息的方式似乎是正确的。你是撞车还是怎么的?能发一下logcat信息吗?

    【讨论】:

    • 我在onclick之外创建了一个变量:final String getvalue = e_name.getText().toString();我称它为 mTask.execute(getvalue);但我收到了这个错误:04-10 17:46:01.736: E/AndroidRuntime(4556): java.lang.RuntimeException: Unable to start Activity ComponentInfo{com.example.tranja/com.example.turanja.register}: java .lang.NullPointerException
    • 您不应该使用 onclick 从 edittext 中提取文本吗?请发布整个 logcat,包括所有崩溃消息。
    【解决方案3】:

    您可以简单地在 doInBackGround() 方法中直接使用这两行代码,然后从这些字段中提取 EditText 的内容。

    final EditText e_id=(EditText) findViewById(R.id.editText1);
    final EditText e_name=(EditText) findViewById(R.id.editText2);
    
    // Extracting content
    String eid = e_id.getText().toString();
    String ename = e_name.getText().toString();
    

    这是最简单的解决方案。另一种选择是通过 AsyncTask 构造函数传递它们。

    【讨论】:

      猜你喜欢
      • 2018-11-22
      • 1970-01-01
      • 2013-07-13
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多