无法从edittext检索值
嗨我想从我的编辑文本检索某些值。无法从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;
}
}
无法访问UI元素从后台线程从doInBackground.you意味着必须通过的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;
}
}
您也可以使用onPreExecute()例程来访问UI元素,但此方法可能更好。另外,我强烈建议您在需要时确保currentPass&changedPass为空。如果您在用户运行此异步任务后不重新使其无效,则它们将无法再次运行,因为变量仍会保存旧值。 –
那么如何在doInBackground中访问currentPass&changedPass?我认为我们需要使用一些额外的类级静态变量来获取'doInBackground'中的currentPass&changedPass值,这并不是在应用程序中使用更多变量或静态变量的最佳实践。 –
那么这取决于你想要做什么。如果你不需要知道它们的值,但只需要知道它们的设置,那么你可以通过onExecute()来访问它们,并决定是否继续。除非我们使用你的答案,否则很难将它们的值传递给doInBackground()方法。 –
你不能做的事情像下面...'的EditText currentPassword =(EditText上)findViewById(R.id.edit_current);'内' 'AsyncTask'的doInBackground(...)方法。使用'AysncTask'的'onPreExecute()'方法来访问UI元素。 – Squonk