2014-02-08 117 views
0

我正在尝试开发更新表单。我在展示性别的微调值方面遇到问题。在我的MySql数据库中,性别的值是男性。现在,我必须在我的微调器中显示该值。如何从MySql数据库检索微调器数据的值

下面是编辑患者的代码。

 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

您的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); 
} 
+0

我已经在我的XML。我的问题是,我必须从数据库中检索该性别微调器的值并将其显示在微调器中。例如,用户注册了他的名字:艾伦和性别:男性。现在我想在我的EditPatient中检索该微调器(它是男性)的值。我怎样才能做到这一点? – user3226149

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)); 

希望它能帮助。

+0

在哪里放? – user3226149

+0

我编辑的答案,请投票并接受答案,如果它适合你。 :)欢呼声 – SMR

+0

我希望这会工作。我试过你的解决方案,但我不知道它是否工作,因为我的应用程序因为另一个问题而崩溃。这里.. http://stackoverflow.com/questions/21642138/android-error-in-passing-id-of-selected-item-in-listview-using-contextmenu?noredirect=1#comment32707307_21642138 – user3226149

相关问题