简体   繁体   English

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

[英]Pass variable in activity to activity fragment

I want to pass a variable in LoadAllProduct extends AsyncTask to Fragment1 extends Fragment but i dont know how can i do. 我想在LoadAllProduct中传递变量,将AsyncTask扩展为Fragment1,扩展Fragment,但我不知道该怎么办。 This is my portion of code : 这是我的代码部分:

 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;
    }

/** * After completing background task Dismiss the progress dialog * **/ / ** *完成后台任务后,关闭进度对话框* ** /

        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() {


            }
        });

    }



}

} }

//this is the class fragment //这是类片段

  public class Fragment1.java extends Fragment {        
  }  

Create a bundle and pass it to the fragment on the intent as an extra. 创建一个捆绑包,并将其作为附加传递给意图上的片段。 Then on the Fragment1 on the OnCreate/OnCreateView/OnActivityCreated method you can get that bundle and assign it to a private variable in that class to use it. 然后,在Fragment1的OnCreate / OnCreateView / OnActivityCreated方法上,您可以获取该捆绑包并将其分配给该类中的私有变量以使用它。

You can use Bundle to pass variables between activities and fragments. 您可以使用Bundle在活动和片段之间传递变量。

You have here the answser for your question. 您在这里问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM