如何解决为什么Getter和Setter在Android中不适用于Arraylist
我是Android新手。我有2个类,并且正在一个类中生成一个ArrayList,然后使用setter方法将其传递给另一个。
问题:我希望仅在单击按钮后发生的数组列表中接收到数据后才显示列表视图。我无法访问getter方法的返回结果,也无法访问getter方法中的任何其他变量。
第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);
}
}
第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上的此文档
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。