我发现在StackOverflow上只有一些与此问题相关的文章尝试了各种组合的所有解决方案,但无法让RecyclerView
显示我的RecyclerView.Adapter
填充了我可以验证的数据,并在调试模式下查看列表,但它只是不会显示信息。
我的代码(我很难粘贴XML布局文件):
public class RecipeDetailActivity extends AppCompatActivity {
private RecipeModel mRecipe;
private ArrayList<IngredientModel> mIngredientList = new ArrayList<>();
private IngredientListAdapter ingredientListAdapter;
private RecyclerView rvIngredientList;
private boolean mTwoPane;
private ListView listView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
rvIngredientList = (RecyclerView)findViewById(R.id.rv_ingredient_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvIngredientList.setLayoutManager(layoutManager);
SampleRecycler sampleRecycler = new SampleRecycler(); // empty RecyclerView.Adapter
rvIngredientList.setAdapter(sampleRecycler);
mRecipe = getIntent().getParcelableExtra("com.wernerraubenheimer.bakingapp.data.RecipeModel");
mIngredientList = mRecipe.getIngredients();
ingredientListAdapter = new IngredientListAdapter(mIngredientList);
ingredientListAdapter.notifyDataSetChanged();
// Up until this point the RecyclerVie.Adapter is populated with data - debug mode I can see the list
rvIngredientList.swapAdapter(ingredientListAdapter, false);
}
}
public class IngredientListAdapter extends RecyclerView.Adapter<IngredientListAdapter.IngredientViewHolder> {
private ArrayList<IngredientModel> mIngredientList;
private Context mContext;
private boolean mTwoPane;
public IngredientListAdapter(ArrayList<IngredientModel> ingredientList) {
mIngredientList = ingredientList;
}
@Override
public IngredientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
int layoutIdForListItem = R.layout.ingredient_list_item;
LayoutInflater inflater = LayoutInflater.from(mContext);
boolean attachImmediatelyToParent = false;
View view = inflater.inflate(layoutIdForListItem, parent, attachImmediatelyToParent);
IngredientViewHolder viewHolder = new IngredientViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(IngredientViewHolder holder, int position) {
IngredientModel ingredient = mIngredientList.get(position);
holder.bind(mContext, ingredient);
}
@Override
public int getItemCount() {
if (mIngredientList != null) {
return 0;
} else {
return mIngredientList.size();
}
}
static class IngredientViewHolder extends RecyclerView.ViewHolder {
private CardView cardviewContainer;
private TextView tvIngredientName;
private TextView tvIngredientQuantity;
private TextView tvIngredientMeasure;
private Context mContext;
private IngredientModel mIngredient;
IngredientViewHolder(View itemView) {
super(itemView);
//cardviewContainer = (CardView)itemView.findViewById(R.id.card_view_ingredient);
tvIngredientName = (TextView)itemView.findViewById(R.id.text_view_ingredient_name);
tvIngredientQuantity =(TextView)itemView.findViewById(R.id.text_view_quantity);
tvIngredientMeasure = (TextView)itemView.findViewById(R.id.text_view_measure);
}
void bind(Context context, IngredientModel ingredientModel) {
mContext = context;
mIngredient = ingredientModel;
tvIngredientName.setText(mIngredient.getIngredient());
tvIngredientQuantity.setText(mIngredient.getQuantity());
tvIngredientMeasure.setText(mIngredient.getMeasure());
}
}
}
答案 0 :(得分:0)
虽然很难从您粘贴的代码中解读, 看起来问题在于以下功能:
@OneOf(values = "12-14m, 16m, 18m")
private String size;
将条件更改为@Override
public int getItemCount() {
if (mIngredientList != null) {
return 0;
} else {
return mIngredientList.size();
}
}
可以解决问题。
此外,在if (mIngredientList == null) {
方法中,只有以下代码才足以显示列表:
RecipeDetailActivity.onCreate()