我一直在努力试图在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);
}