2016-11-07 74 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

你可以更新你的问题,以便我可以看到你有什么做了什么? –