[英]Why is Getter and Setter not working for Arraylist in Android
我是安卓新手。 我有 2 个类,我在一个类中生成一个 ArrayList 并使用 setter 方法将它传递给另一个类。
问题:我希望仅在单击按钮后发生的 arraylist 中接收到数据后才显示列表视图。 我无法访问 getter 方法的返回结果,也无法访问 getter 方法中的任何其他变量。
第 1 类:搜索成分(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);
}
}
第 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);
}
}
您正在创建SearchIngredients
另一个实例,您需要使用传递给Ingredients
构造函数的实例
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);
}
为了让另一个类可以访问一个属性或方法,它必须是公共的。
请参阅 w3schools.com 上的此文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.