0

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

首先,当我没有输入任何内容时,我敬酒以通知我没有文本输入并且它有效!

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

显示 toast / 检索文本的代码:

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;
        }
    }
4

1 回答 1

0

您不能从后台线程访问 UI 元素意味着来自 doInBackground。您必须将 EditText 值作为参数传递给 doInBackground,如下所示:

首先在 ArrayList 中添加 EditText vlaues 为:

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

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

现在将此列表传递给 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;
                }
}
于 2012-11-26T03:36:03.977 回答