0

こんにちは、編集テキストから特定の値を取得しようとしています。

まず、何も入力していないときに、テキスト入力がないことを通知するために乾杯しました。

次に、テキストを入力すると、今度はトーストが再び表示されます! テキスト入力がある場合..

トーストを表示する/テキストを取得するコード:

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 値を追加します。

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 に答える