将活动中的变量传递给活动片段

时间:2014-07-07 13:55:24

标签: java android android-asynctask fragment android-fragmentactivity

我想在LoadAllProduct中传递一个变量,将AsyncTask传递给Fragment1扩展Fragment,但我不知道该怎么办。 这是我的代码部分:

 public class MainChauffeur extends FragmentActivity implements TabListener{

 JSONParser jParser = new JSONParser();
            .
            .
            .
 @Override
 protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.activity_main_chauffeur);
            LoadAllProduct a=new LoadAllProduct();
    a.execute();
 } 
 class LoadAllProduct extends AsyncTask<String, String, String> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(c);
        pDialog.setMessage("Chargement Travail...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
        // Check your log cat for JSON reponse
        Log.d("All Product: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {

                products = json.getJSONArray(TAG_PRODUCTS);
                Intent t=new Intent();
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                     id = c.getString(TAG_PID);
                     this.name = c.getString(TAG_NAME);
                                             /**
                                                ** i want to pass this.name in the Fragment1.java **
                                             **/       
                                            // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();


                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);
                    productsList.add(map);
                }
            } else {

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

/ **         *完成后台任务后,关闭进度对话框 * ** /

        protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {


            }
        });

    }



}

}

//这是类片段

  public class Fragment1.java extends Fragment {        
  }  

2 个答案:

答案 0 :(得分:0)

创建一个包并将其作为附加内容传递给intent上的片段。然后在OnCreate / OnCreateView / OnActivityCreated方法的Fragment1上,您可以获取该包并将其分配给该类中的私有变量以使用它。

答案 1 :(得分:0)

您可以使用Bundle在活动和片段之间传递变量。

您的问题有here个答案。