2016-11-07 75 views
0

夢幻般的morg。在此代碼下面,從mysql數據庫獲取數據並顯示到EditText元素中。使用此asyn tesknew checkUserPermission().execute("");db獲取數據的工作良好沒有問題。無法從編輯文本元素中獲得值

問題是 我想從code做一些計算和dispaly在另一個Edittext。所以我需要值這就是爲什麼我從db。而OnCreate()的數據從db(其工作)獲得數據。每當我打電話給calculatePL();的方法,我都無法獲得價值。

logcat的: System.out: Empty Value

爲什麼其空或東西。但高於我的edittext元素的值爲 。

...some declaration of variables and etc.... 
       public void onCreate(Bundle SavedInstanceState) { 
         super.onCreate(SavedInstanceState); 
         setContentView(R.layout.five_activity); 

         new checkUserPermission().execute(""); //call here 
         calculatePL();//call the method 
     } 

    class checkUserPermission extends AsyncTask<String, String, String> { 

    private ProgressDialog Dialog = new ProgressDialog(Five_Activity.this); 

    @Override 
    protected void onPreExecute() { 
     Dialog.setMessage("Please wait.."); 
     Dialog.show(); 
     super.onPreExecute(); 
     userid = (TextView)findViewById(R.id.userID); 
     uid = userid.getText().toString(); 
     System.out.println(uid); 
    } 

    @Override 
    protected String doInBackground(String... arg0) { 
     ArrayList<NameValuePair> values = new ArrayList<NameValuePair>(); 

     values.add(new BasicNameValuePair("userid", uid)); 

     try { 
      DefaultHttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://192.168.1.13:8090/stat_api/shiftClose.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(values)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is2 = entity.getContent(); 
      Log.i("TAG", "Connection Successful"); 

     } catch (Exception e) { 
      Log.i("TAG", e.toString()); 
      //Invalid Address 
     } 

     try { 
      BufferedReader reader2 = new BufferedReader(new InputStreamReader(is2, "iso-8859-1"), 8); 
      StringBuilder sb2 = new StringBuilder(); 
      while ((line2 = reader2.readLine()) != null) { 
       sb2.append(line2 + "\n"); 
      } 
      is2.close(); 
      result2 = sb2.toString(); 

      JSONObject json_data2 = new JSONObject(result2); 
      code2=(json_data2.getString("code")); 

      Allvalues = code2; 
      String[] splited = Allvalues.split("\\s+"); 

      Totalkm=splited[0]; 
      discountamt=splited[1]; 
      receviedamt=splited[2]; 
      totalamt=splited[3]; 
      expen=splited[4]; 

      //Log.d("Splited String ", "Splited String" + totalamt+expen); 

      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        totkm.setText(Totalkm); 
        discount.setText(discountamt); 
        recamt.setText(receviedamt); 
        totamt.setText(totalamt); 
        expenses.setText(expen); 
       } 
      }); 

      Log.i("TAG", "Result Retrieved"); 

     } catch (Exception e) { 
      Log.i("TAG", e.toString()); 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    protected void onPostExecute(String result){ 
     // Close progress dialog 
     Dialog.dismiss(); 
    } 
} 

public void calculatePL(){ 
     try { 
      String a_value =totamt.getText().toString().trim(); 
      System.out.println(a_value); 

     }catch(NumberFormatException numberEx) 
     { 
      System.out.println(numberEx); 
     } 
    } 
+0

'的setText()'不應內部'doInBackground()'方法來完成。你應該在'onPostExecute()'中做。所有與UI相關的更改都應在'onPostExecute()'內進行。 –

+0

oh好兄弟我會改變和測試'onPostExecute()'@PrathameshToradmal – karthi

回答

0

在您的asyncTask中重寫protected void onPostExecute,並在此處調用calculatePl()。你也應該在onPostExecute中設置Edittext的文本,因爲這個方法是主線程,你不需要使用runOnUIThread

EDIT與示例代碼:

class checkUserPermission extends AsyncTask<String, String, String> { 

     private ProgressDialog Dialog = new ProgressDialog(Five_Activity.this); 

     @Override 
     protected void onPreExecute() { 
      Dialog.setMessage("Please wait.."); 
      Dialog.show(); 
      super.onPreExecute(); 
      userid = (TextView)findViewById(R.id.userID); 
      uid = userid.getText().toString(); 
      System.out.println(uid); 
     } 

     @Override 
     protected String doInBackground(String... arg0) { 
      ArrayList<NameValuePair> values = new ArrayList<NameValuePair>(); 

      values.add(new BasicNameValuePair("userid", uid)); 

      try { 
       DefaultHttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://192.168.1.13:8090/stat_api/shiftClose.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(values)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is2 = entity.getContent(); 
       Log.i("TAG", "Connection Successful"); 

      } catch (Exception e) { 
       Log.i("TAG", e.toString()); 
       //Invalid Address 
      } 

      try { 
       BufferedReader reader2 = new BufferedReader(new InputStreamReader(is2, "iso-8859-1"), 8); 
       StringBuilder sb2 = new StringBuilder(); 
       while ((line2 = reader2.readLine()) != null) { 
        sb2.append(line2 + "\n"); 
       } 
       is2.close(); 
       result2 = sb2.toString(); 

       JSONObject json_data2 = new JSONObject(result2); 
       code2=(json_data2.getString("code")); 

       Allvalues = code2; 


      } catch (Exception e) { 
       Log.i("TAG", e.toString()); 
       e.printStackTrace(); 
      } 
      return Allvalues; 
     } 
     protected void onPostExecute(String result){ 

      String[] splited = result.split("\\s+"); 

      Totalkm=splited[0]; 
      discountamt=splited[1]; 
      receviedamt=splited[2]; 
      totalamt=splited[3]; 
      expen=splited[4]; 


      totkm.setText(Totalkm); 
      discount.setText(discountamt); 
      recamt.setText(receviedamt); 
      totamt.setText(totalamt); 
      expenses.setText(expen); 
      // Close progress dialog 
      Dialog.dismiss(); 
      calculatePL(); 
     } 
    } 
+0

你能詳細解釋嗎?我更新我的代碼 – karthi

+0

@karthi調用'onPostExecute'方法,然後'doInBackground'完成。所以你可以保證在你從服務器獲取數據後調用'calculatePl()'。使用您提供的當前代碼,您在從服務器獲取數據之前調用calculatePL(),這就是爲什麼它返回空值。 –

+0

是的,我不知道我得到的值之前calculatePl()執行(我認爲在完成'checkUserPermission'' calculatePI()'跑了)。這是問題,但如何解決?我不知道@Vygintas先生 – karthi

-1

確保totamt被聲明爲一個全球性的。嘗試記錄totamt的值或相同的對象。最後檢查你宣佈的地方。

+0

是的,我宣佈全球'EditText totamt = NULL;' – karthi

+0

你創建了對象嗎? –

+0

在像你的用戶ID那樣調用函數之前? (的TextView)findViewById(R.id.userID); –

1

您的checkUserPermission在後臺執行。並立即致電calculatePL(),以便您的主線程不會等待checkUserPermission執行完成。

你需要做的是,讓等待你的主線程,以便完全執行checkUserPermissioncalculatePL()將被調用。您可以通過添加ProgressDialog來實現。在onPreExecute()中顯示ProgressDialog並將其解僱onPostExecute()

希望它能完成您的工作。

+0

好吧我檢查你的概念 – karthi

+0

不是我試過你的方式,但仍然是同樣的問題 – karthi

+0

你可以更新你的問題,以便我可以看到你有什麼做了什麼? –