无法从edittext检索值

时间:2012-11-26 03:31:15

标签: android android-edittext

您好我正在尝试从我的编辑文本中检索某些值。

首先,当我没有输入任何东西时,我做了一个祝酒词通知我没有文字输入而且它有效!

然后当我输入文字时,这次再次出现祝酒词!当有文字输入..

显示吐司/检索文本的代码:

public class ResetPassword extends Activity implements OnClickListener{

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    private static final String TAG_PASSWORD = "password";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PASS = "pass";

    String enteredPassword;
    String changedPass;
    String currentPass;
    //String password;

    // products JSONArray
    JSONArray products = null;

    //private static String url_member_login = "http://10.0.2.2/android_connect/get_login.php";
    private static String url_login = "http://10.0.2.2/android_connect/change_password.php";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.password);

        this.setRequestedOrientation(
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        ImageButton home = (ImageButton)findViewById(R.id.btn_home);
        home.setOnClickListener(this);


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

            public void onClick(View v) 
                {
                if (currentPass != null && changedPass != null)
                {
                   new ResetPass().execute();
                }     
                else
                {
                    /** A TEMPORARY DISPLAY FROM CUSTOM TOAST **/
                    LayoutInflater inflater = getLayoutInflater();
                    View layout = inflater.inflate(R.layout.customtoast,
                                                   (ViewGroup) findViewById(R.id.toast_layout_root));

                    TextView text1 = (TextView) layout.findViewById(R.id.txt_engRecords);
                    TextView text2 = (TextView) layout.findViewById(R.id.txt_chiRecords);

                    text1.setText("Feature is temporarily unavailable");
                    text2.setText("Unable to update ");

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.setView(layout);
                    toast.show();               

                }

                }});

    }

    /**
     * Background Async Task to  Save product Details
     * */

    class ResetPass extends AsyncTask<String, String, String> {

        /**
         * Saving product
         * */
        protected String doInBackground(String... args) {

            // check json success tag
            try {
                // Building Parameters

                   EditText currentPassword = (EditText) findViewById(R.id.edit_current);
                    currentPass = currentPassword.getText().toString();

                    EditText newPassword = (EditText) findViewById(R.id.edit_new);
                    changedPass = newPassword.getText().toString();

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair(TAG_PASSWORD, changedPass));

                // sending modified data through http request
                // Notice that update product url accepts POST method
                JSONObject json = jParser.makeHttpRequest(url_login,
                        "GET", params);

                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    finish();
                } else {
                    // failed to update product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

您无法从后台线程访问UI元素来自doInBackground.y必须将EditText值作为参数传递给doInBackground:

首先在ArrayList中添加EditText vlaues:

public static List<String> arrlist = new ArrayList<String>();

    arrlist.add(currentPassword.getText().toString());
    arrlist.add(newPassword.getText().toString());

现在将此List传递给AsyncTask:

new ResetPass().execute(arrlist);

将AsyncTask类更改为:

 class ResetPass extends AsyncTask<ArrayList<String>, String, String> {

        /**
         * Saving product
         * */
        protected String doInBackground(ArrayList<String>... args) {

                    // check json success tag
                  try {
                       // Building Parameters
                      ArrayList<String> result = new ArrayList<String>();
                      result = passing[0]; //get passed arraylist
                      currentPass = result.get(0); // get old pass from ArrayList
                      changedPass = result.get(1);// get new pass from ArrayList
                      //...your code here
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    return null;
                }
}
相关问题