通过适配器膨胀JSON数据

时间:2019-06-18 00:24:17

标签: android json xml

我正在尝试将json数据添加到我的列表项中,并且出现黑屏。我要显示的数据来自嵌套的json数组。因此,我已经使用改造和gson创建了对象。我已经制作了2个对象(蛋糕和配料),并且配料是List <>类型的。我还提供了json文件的图像。

public class CakesItem  {

    @SerializedName("id")
    private int mId;

    @SerializedName("name")
    private String mName;

    @SerializedName("ingredients")
    private List<Ingredient> mIngredients;


    public int getmId() {
        return mId;
    }

    public String getmName() {
        return mName;
    }

    public List<Ingredient> getmIngredients() {
        return mIngredients;
    }


}
public class Ingredient implements Parcelable {


    private double quantity;

    private String measure, ingredient;



    public String getmMeasure() {
        return measure;
    }

    public double getmQuantity() {
        return quantity;
    }


    public String getmIngredient() {
        return ingredient;
    }

}
public class IngredientsAdapter extends RecyclerView.Adapter<IngredientsAdapter.IngredientsHolder> {

    private List<Ingredient> ingredients = new ArrayList<>();


    @NonNull
    @Override
    public IngredientsHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredient_list_item, parent, false);
        return new IngredientsHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final IngredientsHolder Holder, int position) {

        Holder.onBind(ingredients.get(position));

    }

    @Override
    public int getItemCount() {
        return ingredients.size();
    }

    void getIngredientsData(List<Ingredient> ingredients) {
        this.ingredients.clear();
        this.ingredients = ingredients;
        notifyDataSetChanged();
    }

    class IngredientsHolder extends RecyclerView.ViewHolder {


        private final TextView mIngredientsTV;

        IngredientsHolder(@NonNull View itemView) {
            super(itemView);

            mIngredientsTV = itemView.findViewById(R.id.ingredients_list_tv);
        }


        void onBind(Ingredient ingredients) {
            mIngredientsTV.setText(formatString(ingredients.getmIngredient(), String.valueOf(ingredients.getmQuantity()), ingredients.getmMeasure()));
        }

        private String formatString(String i, String q, String m) {
            return " (" + q + " " + m + ") " + i;
        }
    }
}




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_detail);

        mIngredientsList = new ArrayList<>();
        IngredientsAdapter ingredientsAdapter = new IngredientsAdapter();
        ingredientsRecyclerView = findViewById(R.id.ingredients_recycler_view);
        ingredientsRecyclerView.setHasFixedSize(true);
        ingredientsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        ingredientsRecyclerView.setAdapter(ingredientsAdapter);

        ingredientsAdapter.getIngredientsData(mIngredientsList);


        mRecipeImage = findViewById(R.id.recipe_cake_image_iv);
        Drawable myDrawable = getResources().getDrawable(R.drawable.nutella_pie_2);
        mRecipeImage.setImageDrawable(myDrawable);

        mCakeTitle = findViewById(R.id.cake_title);
        mCakeTitle.setText("Nutella Pie");

    }

1 个答案:

答案 0 :(得分:0)

您在哪里将JSON解析为对象?从代码中,您只需为适配器设置一个空列表。
尝试: 在MainActivity中:

@Override
public void onItemClicked(CakesItem cakesItem) {
    Intent detailIntent = new Intent(this, recipe_detail.class);
    detailIntent.putExtra("Ingredients", cakesItem.getmIngredients());
    startActivity(detailIntent);
}

在配方详细信息中:

private List<Ingredient> mIngredientsList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    mIngredientsList = getIntent().getSerializableExtra("Ingredients");;
    IngredientsAdapter ingredientsAdapter = new IngredientsAdapter();
    ingredientsAdapter.getIngredientsData(mIngredientsList);
    ...
}