Java 为什么Getter和Setter在Android中不适用于Arraylist
我是Android新手。我有两个类,我在一个类中生成一个ArrayList,并使用setter方法将其传递给另一个类 问题:我希望listview仅在arraylist中收到数据后显示,而arraylist是在单击按钮后出现的。我无法访问getter方法的返回结果,也无法访问getter方法中的任何其他变量 类别1:SearchComponents getFoodlist、setFoodlistJava 为什么Getter和Setter在Android中不适用于Arraylist,java,android,android-studio,arraylist,getter-setter,Java,Android,Android Studio,Arraylist,Getter Setter,我是Android新手。我有两个类,我在一个类中生成一个ArrayList,并使用setter方法将其传递给另一个类 问题:我希望listview仅在arraylist中收到数据后显示,而arraylist是在单击按钮后出现的。我无法访问getter方法的返回结果,也无法访问getter方法中的任何其他变量 类别1:SearchComponents getFoodlist、setFoodlist public class SearchIngredients extends AppCompatAc
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类:
您正在创建另一个SearchComponents实例,需要使用传递给Components构造函数的实例
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上的此文档
因此,在类1中的setter方法中,来自类2的数据显示为变量this.foodlist。这不会在整个课程中设置this.foodlist的值吗?为什么getter方法不显示数据?
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);
}