通过适配器膨胀 json 数据
Posted
技术标签:
【中文标题】通过适配器膨胀 json 数据【英文标题】:Inflating json data via Adapter 【发布时间】:2019-10-31 13:53:39 【问题描述】:我正在尝试将 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");
【问题讨论】:
您是否为您的RecyclerView
设置了物品装饰器? android.jlelse.eu/…
不,我没有添加物品装饰器,应该吗?
你绝对需要。尝试添加一个。
【参考方案1】:
您在哪里将 JSON 解析为对象?从代码中,您刚刚为您的适配器设置了一个空列表。 尝试: 在 MainActivity:
@Override
public void onItemClicked(CakesItem cakesItem)
Intent detailIntent = new Intent(this, recipe_detail.class);
detailIntent.putExtra("Ingredients", cakesItem.getmIngredients());
startActivity(detailIntent);
在recipe_detail中:
private List<Ingredient> mIngredientsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
...
mIngredientsList = getIntent().getSerializableExtra("Ingredients");;
IngredientsAdapter ingredientsAdapter = new IngredientsAdapter();
ingredientsAdapter.getIngredientsData(mIngredientsList);
...
【讨论】:
哦,我认为你正在做一些事情,但如果我在我的 activity_recipe_detail 中实现该行,“json”将不会被解析,因为网络调用是在 Main_activity 上实现的。 github.com/Pureskill75/BakingApp/blob/master/app/src/main/java/… @pureskill75 已编辑,请尝试。 全部完成,谢谢,我用 parcelable 替换了可序列化以上是关于通过适配器膨胀 json 数据的主要内容,如果未能解决你的问题,请参考以下文章
InvocationTargetException 在适配器类中膨胀视图
无条件布局,来自视图适配器的膨胀:应该使用 View Holder 模式
从膨胀的 XML 将适配器设置为 ListView 以打印 PDF - 缺少项目
在列表适配器中膨胀类 com.google.android.youtube.player.YouTubePlayerView 时出错