I am new to Android. I have 2 classes and I am generating an ArrayList in one class and passing it to another using the setter method.
Problem : I want the listview to display only after the data is received in the arraylist which occurs after click the button. I cannot access the getter method's return result nor I can access any other variable in the getter method.
Class 1 : SearchIngredients (getFoodlist, setFoodlist)
public class SearchIngredients extends AppCompatActivity {
//UI
EditText enter_ingredients;
Button search_ingredients, search_recipe;
ListView ingredient_display;
//Variables
private Recipe displayRecipe;
private Instructions instructions;
private ArrayList<String> foodlist;
private JSONArray foodArray;
private ArrayList<String> ingredientList;
private IngredientAdapter ingredientAdapter;
private JSONArray jsonArray;
private boolean stats = false;
String TAG = "SearchIngredients";
public SharedPreferences preferences;
public SharedPreferences.Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
//UI
enter_ingredients = findViewById(R.id.enter_ingredients);
ingredient_display = findViewById(R.id.ingredient_display);
search_ingredients = findViewById(R.id.search_ingredients);
search_recipe = findViewById(R.id.search_recipe);
// variables
foodlist = new ArrayList<String>();
foodArray = new JSONArray();
ingredientList = new ArrayList<String>();
jsonArray = new JSONArray();
preferences = this.getSharedPreferences("dataStatus", MODE_PRIVATE);
editor = preferences.edit();
//click functionality
search_recipe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String food = enter_ingredients.getText().toString();
if (food.equals("")) {
enter_ingredients.setError("Enter the ingredients before searching");
} else {
getIngredients(food);
}
}
});
}
public void getIngredients(String food) {
Ingredients methods = new Ingredients(SearchIngredients.this);
String url = methods.SearchIngredientURL(food);
methods.JsonRequest(url);
}
//Getter and Setter
public ArrayList<String> getFoodlist() {
Log.d("foodArrayGetList", "" + foodlist); // after setting the value still the value is null
return this.foodlist;
}
public void setFoodlist(ArrayList<String> foodlist) { //setting the value for foodlist from Class 2
this.foodlist = new ArrayList<>();
this.foodlist = foodlist;
Log.d("foodArraySetList", "" + this.foodlist);
}
}
Class 2 :
public class Ingredients {
private Context context;
private ArrayList<String> newData;
private JSONArray foodArray;
private ArrayList<String> ingredientsList;
//constructor
public Ingredients(Context context) {
this.context = context;
this.newData = new ArrayList<String>();
this.foodArray = new JSONArray();
this.ingredientsList = new ArrayList<String>();
}
//JSON Object Request
//GET Method from API
public void JsonRequest(String url) {
final SearchIngredients searchIngredients = new SearchIngredients();
JsonArrayRequest arrayRequest = new JsonArrayRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
setFoodArray(response);
Log.d("foodArrayIn", "" + foodArray);
} catch (JSONException e) {
e.printStackTrace();
}
try {
newData = savedData(response);
searchIngredients.setFoodlist(newData); // calling the setter function of Class 1
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Rest Error Recipe", error.toString());
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(arrayRequest);
}
public ArrayList<String> savedData(JSONArray response) throws JSONException {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
//Parse JSON data
String name = (jsonObject.getString("name"));
IngredientName ingredientName = new IngredientName(name);
Log.d("ingredientName", "" + ingredientName.getName());
ingredientsList.add(ingredientName.getName());
}
Log.d("SearchIngre", "" + ingredientsList.toString());
this.setNewData(ingredientsList);
return ingredientsList;
}
//TODO: Change the Log e.
//IngredientName in JSON Format
public String JsonReturn() {
SearchGSON searchGSON = new SearchGSON(context, "Bread, Butter");
Log.e("GSONresult", searchGSON.toString());
return searchGSON.toString();
}
//Getter and Setter
public JSONArray getFoodArray() {
Log.d("foodArraygetI", "" + this.foodArray);
return this.foodArray;
}
public void setFoodArray(JSONArray foodArray) throws JSONException {
for (int i = 0; i < foodArray.length(); i++) {
this.foodArray.put(foodArray.get(i));
}
Log.d("foodArraySetClass", "" + this.foodArray);
}
public ArrayList<String> getNewData() {
Log.d("newDataSet", "" + this.foodArray);
return this.newData;
}
public void setNewData(ArrayList<String> newData) {
this.newData = newData;
Log.d("newDataSet", "" + this.foodArray);
}
}
You're creating another instance of SearchIngredients
, you need to use instance that you passed to Ingredients
constructor
public class Ingredients {
private SearchIngredients searchIngredients;
private ArrayList<String> newData;
private JSONArray foodArray;
private ArrayList<String> ingredientsList;
//constructor
public Ingredients(SearchIngredients searchIngredients) {
this.searchIngredients = searchIngredients;
this.newData = new ArrayList<String>();
this.foodArray = new JSONArray();
this.ingredientsList = new ArrayList<String>();
}
//JSON Object Request
//GET Method from API
public void JsonRequest(String url) {
//REMOVE THIS final SearchIngredients searchIngredients = new SearchIngredients();
JsonArrayRequest arrayRequest = new JsonArrayRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
setFoodArray(response);
Log.d("foodArrayIn", "" + foodArray);
} catch (JSONException e) {
e.printStackTrace();
}
try {
newData = savedData(response);
searchIngredients.setFoodlist(newData); // calling the setter function of Class 1
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Rest Error Recipe", error.toString());
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(searchIngredients);
requestQueue.add(arrayRequest);
}
In order for an attribute or method to be accessible by another class it has to be public.
See this documentation from w3schools.com
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.