返回多一个ArrayList

时间:2018-05-16 10:49:21

标签: java android

我试图处理这个baking JSON

在Android应用程序中

所以这是从链接中带来JSON数据的类:

OpenBakingJsonUtils.java:

    public final class OpenBakingJsonUtils {

    public static ArrayList<ArraysLists> getSimpleBakingStringsFromJson(Context context, String bakingJsonString)
            throws JSONException {

        final String ID = "id";
        final String NAME = "name";
        final String SERVINGS = "servings";
        final String INGREDIENTS = "ingredients";
        final String STEPS = "steps";
        final String QUANTITY = "quantity";
        final String MEASURE = "measure";
        final String INGREDIENT = "ingredient";
        final String IDSTEPS = "id";
        final String SHORTDESCRIPTION = "shortDescription";
        final String DESCRIPTION = "description";
        final String VIDEOURL = "videoURL";
        final String THUMBNAILURL = "thumbnailURL";

        ArrayList<ArraysLists> parsedRecipeData = new ArrayList<ArraysLists>();

        ArrayList<BakingItem> Baking = new ArrayList<BakingItem>();
        JSONArray recipeArray = new JSONArray(bakingJsonString);

        for (int i = 0; i < recipeArray.length(); i++) {
            int id;
            String name;
            int servings;
            double quantity;
            String measure;
            String ingredient;
            int idSteps;
            String shortDescription;
            String description;
            String videoURL;
            String thumbnailURL;

            JSONObject recipeObject = recipeArray.getJSONObject(i);
            id = recipeObject.getInt(ID);
            name = recipeObject.getString(NAME);
            servings = recipeObject.getInt(SERVINGS);

            ArrayList<IngredientsItem> Ingredients = new ArrayList<IngredientsItem>();
            JSONArray ingredientsArray = recipeObject.getJSONArray(INGREDIENTS);

            for(int j = 0 ; j< ingredientsArray.length(); j++) {

                JSONObject ingredientsObject = ingredientsArray.getJSONObject(j);
                quantity = ingredientsObject.getDouble(QUANTITY);
                measure = ingredientsObject.getString(MEASURE);
                ingredient = ingredientsObject.getString(INGREDIENT);
                Ingredients.add(new IngredientsItem(quantity, measure, ingredient));
            }

            ArrayList<StepsItem> Steps = new ArrayList<StepsItem>();
            JSONArray stepsArray = recipeObject.getJSONArray(STEPS);

            for(int j = 0 ; j< stepsArray.length(); j++) {
                JSONObject stepsObject = stepsArray.getJSONObject(j);
                idSteps = recipeObject.getInt(IDSTEPS);
                shortDescription = stepsObject.getString(SHORTDESCRIPTION);
                description = stepsObject.getString(DESCRIPTION);
                videoURL = stepsObject.getString(VIDEOURL);
                thumbnailURL = stepsObject.getString(THUMBNAILURL);
                Steps.add(new StepsItem(idSteps, shortDescription, description, videoURL, thumbnailURL));
            }

            Baking.add(new BakingItem(id, name, servings, Ingredients, Steps));

            parsedRecipeData.add(new ArraysLists(Baking, Ingredients, Steps));

        }

        return parsedRecipeData;

    }
}

如您所见,有3个ArrayList类:

  1. ArrayList<BakingItem>
  2. ArrayList<IngredientsItem>
  3. ArrayList<StepsItem>
  4. 这是每个代码:

    BakingItem.java:

        public class BakingItem implements Parcelable {
    
        private int id;
        private String name;
        private int servings;
        private ArrayList<IngredientsItem> ingredients = new ArrayList<IngredientsItem>();
        private ArrayList<StepsItem> steps  = new ArrayList<StepsItem>();
    
        public BakingItem(int id, String name, int servings, ArrayList<IngredientsItem> ingredients, ArrayList<StepsItem> steps) {
            this.id = id;
            this.name = name;
            this.servings = servings;
            this.ingredients = ingredients;
            this.steps = steps;
        }
    
        @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeInt(id);
            out.writeString(name);
            out.writeInt(servings);
            out.writeTypedList(ingredients);
            out.writeTypedList(steps);
        }
    
        private BakingItem(Parcel in) {
            this.id = in.readInt();
            this.name = in.readString();
            this.servings = in.readInt();
            ingredients = new ArrayList<IngredientsItem>();
            in.readTypedList(ingredients, IngredientsItem.CREATOR);
    
        }
    
        public BakingItem() {
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        public static final Parcelable.Creator<BakingItem> CREATOR = new Parcelable.Creator<BakingItem>() {
            @Override
            public BakingItem createFromParcel(Parcel in) {
                return new BakingItem(in);
            }
            @Override
            public BakingItem[] newArray(int i) {
                return new BakingItem[i];
            }
        };
    
        public int getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        public int getServings() {
            return servings;
        }
    
    }
    

    IngredientsItem.java:

    public class IngredientsItem implements Parcelable {
    
        private double quantity;
        private String measure;
        private String ingredient;
    
        public IngredientsItem(double quantity, String measure, String ingredient) {
            this.quantity = quantity;
            this.measure = measure;
            this.ingredient = ingredient;
        }
    
        @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeDouble(quantity);
            out.writeString(measure);
            out.writeString(ingredient);
        }
    
        private IngredientsItem(Parcel in) {
            this.quantity = in.readDouble();
            this.measure = in.readString();
            this.ingredient = in.readString();
        }
    
        public IngredientsItem() {
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        public static final Parcelable.Creator<IngredientsItem> CREATOR = new Parcelable.Creator<IngredientsItem>() {
            @Override
            public IngredientsItem createFromParcel(Parcel in) {
                return new IngredientsItem(in);
            }
            @Override
            public IngredientsItem[] newArray(int i) {
                return new IngredientsItem[i];
            }
        };
    
        public double getQuantity() {
            return quantity;
        }
    
        public String getMeasure() {
            return measure;
        }
    
        public String getIngredient() {
            return ingredient;
        }
    
    }
    

    以及StepsItem类 第四个是ArraysLists.java,它包含上面的所有3个数组,并由OpenBakingJsonUtils.java返回:

    然后我试图在不同的活动中调用这些JSON数据 所以在loadInBackground中的 MainActivity.java

    Override
            public ArrayList<BakingItem> loadInBackground() {
    
                URL recipeRequestUrl = NetworkUtils.buildUrl();
    
                try {
                    String jsonBakingResponse = NetworkUtils.getResponseFromHttpUrl(recipeRequestUrl);
    
                    ArrayList<ArraysLists> simpleJsonBakingData = OpenBakingJsonUtils.getSimpleBakingStringsFromJson(MainActivity.this, jsonBakingResponse);
    
                    return simpleJsonBakingData;
    
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
    

    我从ArrayList调用返回的OpenBakingJsonUtils.java,在这种情况下是ArraysLists, 然后在doInBackground中的 DetailActivity.java

     @Override
        protected ArrayList<ArraysLists> doInBackground(Object... params) {
            if (params.length == 0) {
                return null;
            }
            URL reviewsRequestUrl = NetworkUtils.buildUrl();
    
            try {
                String jsonReviewResponse = NetworkUtils.getResponseFromHttpUrl(reviewsRequestUrl);
    
                ArrayList<ArraysLists> simpleJsonReviewData = OpenBakingJsonUtils.getSimpleBakingStringsFromJson(DetailsActivity.this, jsonReviewResponse);
    
                return simpleJsonReviewData;
    
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    

    这里我也调用ArrayList的{​​{1}},但是onBindViewHolder中的MainActivity.java和DetailsActivity.java适配器中的问题:

    ArraysLists

    它只是说当我使用由holder.CommentContent.setText(String.valueOf(mCommentsItems.get(position).getQuantity())); 返回的ArraysLists.java时,无法解析在IngredientsItem.java中的方法getQuantity() 那么在使用返回的ArraysLists.java时,如何从BakingItem.java和IngredientsItem.java调用方法呢?

0 个答案:

没有答案