【问题标题】:Pass variable in activity to activity fragment将活动中的变量传递给活动片段
【发布时间】:2014-07-07 13:55:23
【问题描述】:

我想在 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 {        
  }  

【问题讨论】:

  • 我已经回答了你之前的问题,非常相似stackoverflow.com/a/24608867/2291104 主要思想是使您的 AsyncTask 静态并在构造函数中传递数据。
  • 感谢您的快速答复!我很抱歉我的精神错乱!谢谢你的链接!!
  • 我使用了静态类,但变量 this.name 在 MainChauffeur.java 中使用时仍然为空

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


【解决方案1】:

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

【讨论】:

  • 在您的回答中,请尝试提供代码并解释为什么该代码可以解决 OPs 问题,以加深他们的理解。感谢您回答问题。
  • 我已经使用了 putextra 但我要传递的变量仍然为空
【解决方案2】:

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

您有here 回答您的问题。

【讨论】:

  • 感谢您的链接!!我尝试使用您的解决方案,如果我阻止我在这里发帖!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-16
  • 2015-12-04
  • 1970-01-01
相关资源
最近更新 更多