从onPostExecute内部AsyncTask到OnCreate获取结果

问题描述 投票:1回答:2

我正在使用内部AsyncTask从远程数据库中计算平均值,我得到结果,但

问题是:仅在“ onPostExecute”中可用的“平均值”值,我希望在“创建时()”中可以访问此值,因此我可以将其发送到同一Activity中的另一个AsyncTask)>

public class Place_details extends Activity {
    RatingBar PlaceRatingBar;
    UserSessionManager session;
    String ID;

    Double [] Place_rates;
    int Total_place_rates;
    float Average_place_rates;

    // JSON
    JSONParser jsonparser;
    JSONObject JSONObject;
    ProgressDialog ProgressDialog;
    JSONArray jsonArray1;
    int value;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_place_details);    

         PlaceRatingBar = (RatingBar) findViewById (R.id.Place_rating);

         jsonparser = new JSONParser();

        //Session
        session = new UserSessionManager(Place_details.this);

        new getPlaceRating().execute() ;

     // Here I get 0.0 and not the correct Average 
        Toast.makeText(Place_details.this, ""+Average_place_rates, Toast.LENGTH_SHORT).show();


    } // End Of OnCreate    


    public class getPlaceRating extends AsyncTask<String,String,String>{ 

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                ProgressDialog = new ProgressDialog(Place_details.this);
                ProgressDialog.setTitle("Wait....");
                ProgressDialog.setIndeterminate(false);
                ProgressDialog.setCancelable(true);
                ProgressDialog.show();
            }


            @Override
            protected String doInBackground(String...parma) {
                // TODO Auto-generated method stub
                List<NameValuePair> list = new ArrayList<NameValuePair>();

                // passing place_id value
                list.add(new BasicNameValuePair("id",String_Place_id));
                try {
                JSONObject = jsonparser.makeHttpRequest("http://192.168.1.2/Yourguideapplication/Place_rating2.php", "POST", list);
                Log.e("pass 1", "connection success ");
                }
                catch (Exception e) {
                    Log.e("Fail 1", "Fail connection");
                }

                try {
                value = JSONObject.getInt("value");
                if (value==1){
                    //Place Rating
                    jsonArray1 = JSONObject.getJSONArray("Place_rating");
                    Place_rates = new Double[jsonArray1.length()];
                    Total_place_rates =0;
                    for (int i = 0 ; i < jsonArray1.length() ; i++)
                    {
                        JSONObject object = jsonArray1.getJSONObject(i);
                        Place_rates[i] = object.getDouble("Rating_box");
                        Total_place_rates+= Place_rates[i];
                    }               
                } else {
                    value = 0;

                }
                } catch (Exception e){
                    Log.d("ERORR",e.getMessage());
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                if (value == 1){
                    //Place Rating
                    Average_place_rates = (float) (Total_place_rates/jsonArray1.length());
                    PlaceRatingBar.setRating((float) Average_place_rates);
                } else {
                    Toast.makeText(Place_details.this, "Error", Toast.LENGTH_LONG).show();      
            }
                ProgressDialog.dismiss();
            }
        }

}

谢谢

我正在使用内部AsyncTask从远程数据库计算平均值,但是得到的结果是:仅在“ onPostExecute”中可用的平均值,我希望该值可访问...]]

java android android-asynctask
2个回答
3
投票

您可以创建类似

private interface CallbackListener<T> {
     void onComputingFinished(T arg);
}

0
投票

我在将值从onPostExecute传递到公共无效的onClick(View v)时遇到了一些困难。 @ Sara1994c,可以共享工作代码以在此处传递变量吗?我希望以此为参考,以查看应在何处添加上述脚本。

© www.soinside.com 2019 - 2024. All rights reserved.