0

I want to pass a variable in LoadAllProduct extends AsyncTask to Fragment1 extends Fragment but i dont know how can i do. 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 {        
  }  
user192417
  • 56
  • 1
  • 7
  • I have already answered your previous question, which was **very similar**. http://stackoverflow.com/a/24608867/2291104 The main idea is making your AsyncTask static and passing data in the constructor. – David Ferrand Jul 07 '14 at 13:59
  • thank you for your rapid answer!! i'm sorry for the derangment!! thank you for the link!! – user192417 Jul 07 '14 at 14:07
  • i have use the static class but the variable this.name still null when i used it in MainChauffeur.java – user192417 Jul 10 '14 at 04:56

2 Answers2

0

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.

  • In your answers, please try to provide code and an explanation of why that code solves the OPs problem to further their understanding. Thanks for answering the question. – Vulcronos Jul 07 '14 at 14:33
  • i have already use the putextra but the variable that i want to pass still null – user192417 Jul 10 '14 at 04:52
0

You can use Bundle to pass variables between activities and fragments.

You have here the answser for your question.

Community
  • 1
  • 1
João Marcos
  • 3,872
  • 1
  • 19
  • 14