我正在尝试开发更新表单。我在显示性别微调器的值时遇到问题。在我的MySql数据库中,性别的值是Male。现在我必须在我的微调器中显示该值。
以下是编辑病人的代码。
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.gender_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown
_item);
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uid", uid));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_patient_detials, "GET", params);
// check your log for json response
Log.d("Single Patient Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_PATIENT); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this pid found
// Edit Text
inputusername= (EditText) findViewById(R.id.username);
inputpassword = (EditText) findViewById(R.id.password);
inputfname= (EditText) findViewById(R.id.fname);
inputlname = (EditText) findViewById(R.id.lname);
inputcontact = (EditText) findViewById(R.id.contacts);
inputaddress = (EditText) findViewById(R.id.address);
inputdate = (TextView) findViewById(R.id.datetext);//bday
inputgender = (Spinner) findViewById(R.id.gender);
inputage = (TextView) findViewById(R.id.number);
//rdmale = (RadioButton) findViewById(R.id.rdmale);
//rdfemale = (RadioButton) findViewById(R.id.rdfemale);
// display product data in EditText
inputusername.setText(product.getString(TAG_USERNAME));
inputpassword.setText(product.getString(TAG_PASSWORD));
inputfname.setText(product.getString(TAG_FNAME));
inputlname.setText(product.getString(TAG_LNAME));
inputcontact.setText(product.getString(TAG_CONTACT));
inputaddress.setText(product.getString(TAG_ADDRESS));
inputgender.setSelectedItem(product.getString(TAG_GENDER));//it's an error here. help me //how to set value for spinner
inputage.setText(product.getString(TAG_AGE));
inputdate.setText(product.getString(TAG_BIRTHDAY));
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
/**
* Background Async Task to Save product Details
* */
class SaveProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditPatientActivity.this);
pDialog.setMessage("Saving patient ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Saving product
* */
protected String doInBackground(String... args) {
// getting updated data from EditTexts
String username = inputusername.getText().toString();
String password = inputpassword.getText().toString();
String fname = inputfname.getText().toString();
String lname = inputlname.getText().toString();
String contact = inputcontact.getText().toString();
String age = inputage.getText().toString();
String address = inputaddress.getText().toString();
//String birthday = inputbirthday.getText().toString();
String gender = inputgender.getSelectedItem().toString();
String bday = inputdate.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair(TAG_PID, pid));
params.add(new BasicNameValuePair(TAG_USERNAME, username));
params.add(new BasicNameValuePair(TAG_PASSWORD, password));
params.add(new BasicNameValuePair(TAG_FNAME, fname));
params.add(new BasicNameValuePair(TAG_LNAME, lname));
params.add(new BasicNameValuePair(TAG_ADDRESS, address));
params.add(new BasicNameValuePair(TAG_BIRTHDAY, bday));
params.add(new BasicNameValuePair(TAG_AGE, age));
params.add(new BasicNameValuePair(TAG_GENDER, gender));
params.add(new BasicNameValuePair(TAG_CONTACT, contact));
// sending modified data through http request
// Notice that update product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_update_patient,
"POST", params);
// check json success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully updated
Intent i = getIntent();
// send result code 100 to notify about product update
setResult(100, i);
finish();
} else {
// failed to update product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product uupdated
pDialog.dismiss();
}
}
inputgender.setAdapter(adapter);
答案 0 :(得分:0)
您必须在string.xml文件中声明一个字符串数组
<string-array name="gender">
<item>Male</item>
<item>Female</item>
</string-array>
从数据库中检索数据后
if(gender.equalsIgnoreCase("male"){
youeSpinner.setSelection(0);
}
else if(gender.equalsIgnoreCase("female"){
youeSpinner.setSelection(1);
}
答案 1 :(得分:0)
奥莱特!我看到你已经定义了R.array.gender_array
现在你所要做的就是获得男/女 字符串的相应索引。你可以这样做:
if(product.getString(TAG_GENDER).equals("male"))
inputgender.setSelection(0);//if male is at 0 position
else
inputgender.setSelection(1);//if female is at 1 position
您需要输入此代码
inputaddress.setText(product.getString(TAG_ADDRESS));
inputgender.setSelectedItem(......);//HERE REPLACE THIS LINE WITH THE ABOVE CODE
inputage.setText(product.getString(TAG_AGE));
希望它有所帮助。