2016-11-07 16 views
0

ファンタスティックモール。このコードの下でmysqlデータベースからデータを取得し、EditText要素に表示します。dbからのデータの取得に問題はありません。asyn tesknew checkUserPermission().execute("");を使用してください。編集テキスト要素から値を取得できませんでした

問題は、私は別のEdittextcodeとdispalyからいくつかの計算をしたい です。だから私はdb .while OnCreate()からデータを得て、db(その働き)からデータを取得する理由を値thatsが必要です。私がこの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

オハイオ州オハイオ州オハイオオハイオ州ブラウン私は 'onPostExecute()'を使って変更してテストします@PrathameshToradmal – karthi

答えて

0

protected void onPostExecuteをasyncTaskに上書きし、calculatePl()をここに呼び出します。この方法はメインスレッドであり、runOnUIThreadを使用する必要はないため、onPostExecuteにもEdittextのテキストを設定する必要があります。サンプルコードと

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()の実行前にdbから値を取得するのかどうかわかりません( '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を追加するとそれを達成できます。 ProgressDialogonPreExecute()と表示して、onPostExecute()

で却下してください。

+0

あなたのコンセプトを確認していいですか – karthi

+0

いいえ、私はあなたのやり方を試みましたが同じ問題を解決しました – karthi

+0

あなたの質問に新しいコードを入れて、完了? –

関連する問題