E / RecyclerView:没有连接适配器;跳过布局

时间:2016-10-05 09:55:17

标签: android android-recyclerview adapter retrofit2

我在stackoverflow上一直在阅读不同的答案,我试图实现解决方案,但我仍然得到同样的错误,我不知道为什么! :/

这是我的适配器类:

public class RecipesAdapter extends RecyclerView.Adapter <RecipesAdapter.RecipeHolder>{

private List<Recipes> recipesList;
private static Recipes recipe;
private static Intent intent;
private Context context;
private View row;
private RecipeHolder holder;

//Conctructor
public RecipesAdapter(List<Recipes> recipesList)
{
    this.recipesList=recipesList;
}

//create holder (view)
@Override
public RecipeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     row= LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_row, parent,false);
    //Call constructor : Holder
     holder=new RecipeHolder(row);
    return holder;
}

@Override
public void onBindViewHolder(RecipeHolder holder, int position) {

    recipe= recipesList.get(position);

    holder.recipe_title.setText(recipe.getTitle());
    Picasso.with(holder.recipe_image.getContext()).load(recipe.getImage_url()).into(holder.recipe_image);
    holder.recipe_rank.setText(recipe.getSocial_rank().split("\\.",2)[0]);
    holder.recipe_publisher.setText(recipe.getPublisher());
}

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

/*To avoid calling methods findViewById for each view, Android has added a concept called ViewHolder
 to change 'the values' of each View => concept of Recycling */
public class RecipeHolder extends RecyclerView.ViewHolder
{

    private ImageView recipe_image;
    private TextView recipe_title,recipe_rank,recipe_publisher;

    public RecipeHolder(View itemView) {
        super(itemView);
        recipe_image=(ImageView) itemView.findViewById(R.id.recipe_img);
        recipe_title=(TextView) itemView.findViewById(R.id.recipe_title);
        recipe_rank=(TextView) itemView.findViewById(R.id.recipe_rank);
        recipe_publisher=(TextView) itemView.findViewById(R.id.recipe_publisher);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 context = v.getContext();
                 intent = new Intent(context, DetailRecipe.class);
                intent.putExtra("url",recipesList.get(getAdapterPosition()).getF2f_url());
                intent.putExtra("image",recipesList.get(getAdapterPosition()).getImage_url());
                Log.e("log","url is : "+recipe.getF2f_url());
                context.startActivity(intent);
            }});
    }
}
}

onCreate Method

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cookingrecipes); // Adding Toolbar to Main screen toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); Connection_GetData(); }

onResponse方法:连接成功时

  

我使用Retrofit库来获取数据,我调用Display_Recipes()来设置适配器

        `@Override
        public void onResponse(Call<ResultRecipes> call, Response<ResultRecipes> response) {

            recipesList = response.body().getRecipes();
                //Recycler View
                recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
                recyclerView.setHasFixedSize(true);
                /*Performance*/
                recyclerView.setItemViewCacheSize(24);
                recyclerView.setDrawingCacheEnabled(true);
                recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

                recyclerView.setLayoutManager(new GridLayoutManager(CookingRecipes.this, 2));

            /*Treatment - Display*/
                Display_Recipes();
            }`

Display_Recipes()方法

`public void Display_Recipes() {
    //Setup Adapter
    this.recipeAdapter = new RecipesAdapter(recipesList);
    recyclerView.setAdapter(recipeAdapter);
}`

Image Log 非常感谢,

4 个答案:

答案 0 :(得分:3)

在主线程上设置LayoutManager而不是在任何回调中调用是必要的。

按以下方式更新您的onCreate方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cookingrecipes);
        // Adding Toolbar to Main screen
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        //Shifted your recycler view code here

       //Recycler View
       recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
       recyclerView.setHasFixedSize(true);
       /*Performance*/
       recyclerView.setItemViewCacheSize(24);
       recyclerView.setDrawingCacheEnabled(true);
       recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

       recyclerView.setLayoutManager(new GridLayoutManager(CookingRecipes.this, 2));

       // Only pass context to your adapter
       recyclerView.setAdapter(new RecipesAdapter(CookingRecipes.this);

        Connection_GetData();
    }

并更新你的onResponse Callback:

 @Override
 public void onResponse(Call<ResultRecipes> call, Response<ResultRecipes> response) {

         recipesList = response.body().getRecipes();

         /*Treatment - Display*/
         Display_Recipes();
  }

创建方法以在需要传递更新列表时更新适配器中的列表:

public void udpateList(List<Recipes> recipesList){
    this.recipesList = recipesList;
    notifyDataSetChanged();
}

现在从Display_Recipes方法

调用此方法
public void Display_Recipes() {
        //Update Adapter
        udpateList(recipesList);
    }

答案 1 :(得分:1)

  

谢谢所有朋友,我发现了错误,代码是正确的但是要避免这个警告,我们需要在onCreate方法中设置一个空的适配器。在你想要获取数据的方法内部之后,在我的情况下使用Retrofit库:Connection_GetData();

1 - 创建类似

的类适配器
public class AdapterFirstUse extends RecyclerView.Adapter {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

}

@Override
public int getItemCount() {
    return 0;
}

}

2 - 在onCreate(Bundle savedInstanceState)方法中

recyclerView.setAdapter(new AdapterFirstUse());

3 - 内部Connection_GetData()

  

设置我们的适配器

  //Setup Adapter
   recipeAdapter = new RecipesAdapter(recipesList);
   recyclerView.setAdapter(recipeAdapter);

就是这样,谢谢。

答案 2 :(得分:0)

问题可能是适配器没有告诉Range("E" & currLine & ":AQ" & currLine).Interior.Color = 65535 更新其内容。

RecyclerView中添加方法:

RecipeAdapter

然后在将适配器绑定到public void refresh() { notifyDataSetChanged(); } ;

之后调用它
RecyclerView

答案 3 :(得分:0)

通过在初始化适配器时首先输入空列表数据并稍后更新数据来解决我的问题,例如:

在onCreate()中初始化适配器:

onCreate() {

...
initRecyclerView();
...

}

initRecyclerView():

public void initRecyclerView() {

            //setup recyclerview
            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            recyclerView.setHasFixedSize(true);
            /*Performance*/
            recyclerView.setItemViewCacheSize(24);
            recyclerView.setDrawingCacheEnabled(true);
            recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

            recyclerView.setLayoutManager(new GridLayoutManager(CookingRecipes.this, 2));

    //Setup Adapter
    this.recipeAdapter = new RecipesAdapter(new List());//try to set an empty adapter here first
    recyclerView.setAdapter(recipeAdapter);
}

然后在onResponse内:

@Override
public void onResponse(Call<ResultRecipes> call, Response<ResultRecipes> response) {

     //only do data update inside onResponse
     recipesList = response.body().getRecipes();
     recipeAdapter.recipesList = recipesList;
     notifyDataSetChanged();

}