【问题标题】:How to call Activity methods from inside a Fragment [duplicate]如何从片段内部调用活动方法[重复]
【发布时间】:2016-06-08 10:17:24
【问题描述】:

几天来,我一直在努力理解 Android 中的 Fragments。目前我已经用按钮创建了一个菜单,允许用户在片段之间切换。然后,每个片段将允许用户使用特定于该片段的搜索条件从服务器获取 JSON 数组。

理想情况下,每次搜索后,结果都会保留在内存中,以便用户可以使用菜单在片段之间切换。由于片段将使用当前所有位于 MainActivity 中的相同方法,我如何从片段内部调用 MainActivity 中的方法?

我在 Activity 中的方法代码:

  public ArrayList<Eatery> fillArray() {

    String line;
    Eatery eatery = new Eatery("hello","hello","hello","hello","hello",
            "hello","hello","hello","hello","hello");
    ArrayList<Eatery> eateryList = new ArrayList<>();

    if (getConnection() == true) {
        try {

            URL nameURL = new URL("http://sandbox.kriswelsh.com/hygieneapi/hygiene.php?op=s_name&name=Wok%20This%20Way");
            HttpURLConnection postcodeConnection = (HttpURLConnection) nameURL.openConnection();
            InputStreamReader isr = new InputStreamReader(postcodeConnection.getInputStream());
            BufferedReader nameBF = new BufferedReader(isr);

            while ((line = nameBF.readLine()) != null) {
                JSONArray ja = new JSONArray(line);
                for (int i = 0; i < ja.length(); i++) {

                    JSONObject jo = ja.getJSONObject(i);
                    String id = jo.getString("id");
                    String businessName = jo.getString("BusinessName");
                    String addressLine1 = jo.getString("AddressLine1");
                    String addressLine2 = jo.getString("AddressLine2");
                    String addressLine3 = jo.getString("AddressLine3");
                    String postcode = jo.getString("PostCode");
                    String ratingValue = jo.getString("RatingValue");
                    String ratingDate = jo.getString("RatingDate");
                    String lati = jo.getString("Latitude");
                    String longi = jo.getString("Longitude");
                    eatery.setId(id);
                    eatery.setBusinessName(businessName);
                    eatery.setAddressLine1(addressLine1);
                    eatery.setAddressLine2(addressLine2);
                    eatery.setAddressLine3(addressLine3);
                    eatery.setPostcode(postcode);
                    eatery.setRatingValue(ratingValue);
                    eatery.setRatingDate(ratingDate);
                    eatery.setLati(lati);
                    eatery.setLati(longi);
                    eateryList.add(eatery);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        Toast nameToast = Toast.makeText(getApplicationContext(), "Error: No Active Connection", Toast.LENGTH_LONG);
        nameToast.show();
    }

    return (eateryList);
}

【问题讨论】:

  • 创建fragment和activity之间通信的接口
  • 虽然下面的答案适用于您的情况,但我强烈推荐 Marco 的方法,此处解释为 Call an activity method from a fragment。您希望将片段与特定活动分离。 (这就是片段的全部意义!)
  • 非常感谢您的帮助,我确实在寻找现有的解决方案,但由于某种原因,我设法错过了这个简单的解释,只得到了非常复杂的答案。

标签: android android-activity


【解决方案1】:

你可以这样调用;

((MyActivityClass)getActivity()).methodName();

【讨论】:

    【解决方案2】:

    很简单:

    if (getActivity() instanceof MainActivity) {    
        ((MainActivity) getActivity()).yourMethod(...);
    }
    

    【讨论】:

      【解决方案3】:

      正确的通信方式是通过interface。 查看此video 以完成工作。如果您是 Android 开发新手,那么整个系列都值得一试。

      【讨论】:

        猜你喜欢
        • 2013-11-12
        • 1970-01-01
        • 1970-01-01
        • 2021-12-23
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        • 2013-10-20
        相关资源
        最近更新 更多