2017-10-20 79 views
0

我一直在试图让回收视图与改造工作。我似乎从getRecipes()方法中获得了JSON罚款,并且我的日志显示了一些数据在那里。从JSON使用改进和填充RecyclerView ArrayList的问题

但是,当我从onCreate()调用getRecipes()方法时,出现了一些问题。当我检查我的recipeList数组是否在onCreate中包含我的JSON结果时,它告诉我它是空的。为什么它会这样做,如果我的getRecipes()方法中的日志显示数据在那里...?

不知道这是我的回收商视图还是我的改型或其他问题。一直试图找出几天,所以任何意见将不胜感激。

JSON https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json

public class ItemListActivity extends AppCompatActivity { 

private boolean mTwoPane; 
public static final String LOG_TAG = "myLogs"; 
public static List<Recipe> recipeList = new ArrayList<>(); 


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

    getRecipes(); 

    //Logging to check that recipeList contains data 

    if(recipeList.isEmpty()){ 
     Log.d(LOG_TAG, "Is empty"); 
    }else { 
     Log.d(LOG_TAG, "Is not empty"); 
    } 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    toolbar.setTitle(getTitle()); 

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.item_list); 
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(layoutManager); 

    SimpleItemRecyclerViewAdapter simpleItemRecyclerViewAdapter = new SimpleItemRecyclerViewAdapter(recipeList); 

    recyclerView.setAdapter(simpleItemRecyclerViewAdapter); 

    if (findViewById(R.id.item_detail_container) != null) { 

     mTwoPane = true; 
    } 

} 


public void getRecipes(){ 

    String ROOT_URL = "https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/"; 

    Retrofit RETROFIT = new Retrofit.Builder() 
      .baseUrl(ROOT_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    RecipeService service = RETROFIT.create(RecipeService.class); 

    Call<List<Recipe>> call = service.getMyJson(); 
    call.enqueue(new Callback<List<Recipe>>() { 
     @Override 
     public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) { 
      Log.d(LOG_TAG, "Got here"); 
      if (!response.isSuccessful()) { 
       Log.d(LOG_TAG, "No Success"); 
      } 

      Log.d(LOG_TAG, "Got here"); 

      recipeList = response.body(); 

      //Logging to check data is there 
      Log.v(LOG_TAG, "LOGS" + recipeList.size()); 

      for (int i = 0; i < recipeList.size(); i++) { 
       String newString = recipeList.get(i).getName(); 

       Ingredients[] ingredients = recipeList.get(i).getIngredients(); 
       for(int j = 0; j < ingredients.length; j++){ 
        Log.d(LOG_TAG, ingredients[j].getIngredient()); 
       } 

       Steps[] steps = recipeList.get(i).getSteps(); 
       for(int k = 0; k < steps.length; k++){ 
        Log.d(LOG_TAG, steps[k].getDescription()); 
       } 

       Log.d(LOG_TAG, newString); 


      } 


     } 

     @Override 
     public void onFailure(Call<List<Recipe>> call, Throwable t) { 
      Log.e("getRecipes throwable: ", t.getMessage()); 
      t.printStackTrace(); 

     } 
    }); 


} 



public class SimpleItemRecyclerViewAdapter 
     extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> { 

    private final List<Recipe> mValues; 

    public SimpleItemRecyclerViewAdapter(List<Recipe> items) { 
     mValues = items; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.item_list_content, parent, false); 
     return new ViewHolder(view); 
    } 

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

     holder.mItem = mValues.get(position); 
     holder.mContentView.setText(mValues.get(position).getName()); 

     holder.mView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mTwoPane) { 
        Bundle arguments = new Bundle(); 
        arguments.putString(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.getId()); 
        ItemDetailFragment fragment = new ItemDetailFragment(); 
        fragment.setArguments(arguments); 
        getSupportFragmentManager().beginTransaction() 
          .replace(R.id.item_detail_container, fragment) 
          .commit(); 
       } else { 
        Context context = v.getContext(); 
        Intent intent = new Intent(context, ItemDetailActivity.class); 
        intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.getId()); 

        context.startActivity(intent); 
       } 
      } 
     }); 
    } 

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

    public class ViewHolder extends RecyclerView.ViewHolder { 
     public View mView; 
     public TextView mContentView; 
     public Recipe mItem; 


     public ViewHolder(View view) { 
      super(view); 
      mView = view; 
      mContentView = (TextView) view.findViewById(R.id.content); 

     } 

     @Override 
     public String toString() { 
      return super.toString() + " '" + mContentView.getText() + "'"; 
     } 
    } 
} 

RecipeService

public interface RecipeService { 
@GET("baking.json") 
Call<List<Recipe>> getMyJson();} 

模型

配方

public class Recipe{ 

private Ingredients[] ingredients; 

private String id; 

private String servings; 

private String name; 

private String image; 

private Steps[] steps; 

public Ingredients[] getIngredients() 
{ 
    return ingredients; 
} 

public void setIngredients (Ingredients[] ingredients) 
{ 
    this.ingredients = ingredients; 
} 

public String getId() 
{ 
    return id; 
} 

public void setId (String id) 
{ 
    this.id = id; 
} 

public String getServings() 
{ 
    return servings; 
} 

public void setServings (String servings) 
{ 
    this.servings = servings; 
} 

public String getName() 
{ 
    return name; 
} 

public void setName (String name) 
{ 
    this.name = name; 
} 

public String getImage() 
{ 
    return image; 
} 

public void setImage (String image) 
{ 
    this.image = image; 
} 

public Steps[] getSteps() 
{ 
    return steps; 
} 

public void setSteps (Steps[] steps) 
{ 
    this.steps = steps; 
} 

@Override 
public String toString() 
{ 
    return "[ingredients = "+ingredients+", id = "+id+", servings = "+servings+", name = "+name+", image = "+image+", steps = "+steps+"]"; 
}} 

成分

public class Ingredients{ 
private String measure; 

private String ingredient; 

private String quantity; 

public String getMeasure() 
{ 
    return measure; 
} 

public void setMeasure (String measure) 
{ 
    this.measure = measure; 
} 

public String getIngredient() 
{ 
    return ingredient; 
} 

public void setIngredient (String ingredient) 
{ 
    this.ingredient = ingredient; 
} 

public String getQuantity() 
{ 
    return quantity; 
} 

public void setQuantity (String quantity) 
{ 
    this.quantity = quantity; 
} 

@Override 
public String toString() 
{ 
    return "[measure = "+measure+", ingredient = "+ingredient+", quantity = "+quantity+"]"; 
}} 

步骤

public class Steps{ 

private String id; 
    private String shortDescription; 

    private String description; 

    private String videoURL; 

    private String thumbnailURL; 

    public String getId() 
    { 
     return id; 
    } 

    public void setId (String id) 
    { 
     this.id = id; 
    } 

    public String getShortDescription() 
    { 
     return shortDescription; 
    } 

    public void setShortDescription (String shortDescription) 
    { 
     this.shortDescription = shortDescription; 
    } 

    public String getDescription() 
    { 
     return description; 
    } 

    public void setDescription (String description) 
    { 
     this.description = description; 
    } 

    public String getVideoURL() 
    { 
     return videoURL; 
    } 

    public void setVideoURL (String videoURL) 
    { 
     this.videoURL = videoURL; 
    } 

    public String getThumbnailURL() 
    { 
     return thumbnailURL; 
    } 

    public void setThumbnailURL (String thumbnailURL) 
    { 
     this.thumbnailURL = thumbnailURL; 
    } 

    @Override 
    public String toString() 
    { 
     return "[id = "+id+", shortDescription = "+shortDescription+", description = "+description+", videoURL = "+videoURL+", thumbnailURL = "+thumbnailURL+"]"; 
    }} 

日志

https://gist.github.com/2triggers/12b6eeb32ed8909ab50bbadd4742d7f7

+0

尝试调试并检查Log.v(LOG_TAG,“LOGS”+ recipeList.size());大小是多少? –

回答

0

这是因为在将配方列表发送到空的适配器并从getRecipes方法填充配方列表后,即使它已填充之前,您仍将配方列表发送到适配器中,您可能想知道您已声明getRecipes方法之前,即使您将配方列表分配到适配器,以便如何它是空的,是的,但事实是你的getRecipes在后台线程工作,所以即使在你的配方列表填充之前,你的适配器分配发生在主线程,所以你基本上分配空白列表中,您可以做的一件事是在适配器何时更改数据或配方清单中填充了getRecipe方法内的数据时通知您。

当你在这之后分配recipelist = response.body的权利,你可以通知适配器

recipelist = response.body; 
在getRecipes方法

后向右移动这两条线

SimpleItemRecyclerViewAdapter simpleItemRecyclerViewAdapter = new SimpleItemRecyclerViewAdapter(recipeList); 
recyclerView.setAdapter(simpleItemRecyclerViewAdapter); 

+0

YEEEESSSSSSSSS!谢谢 ! – Rogerto

1

这将是总是空的,因为这条线将得到来自服务器的响应之前执行。

if(recipeList.isEmpty()){ 
     Log.d(LOG_TAG, "Is empty"); 
    }else { 
     Log.d(LOG_TAG, "Is not empty"); 
    } 

在此行之后,最好调用它recipeList = response.body();

SimpleItemRecyclerViewAdapter simpleItemRecyclerViewAdapter = new SimpleItemRecyclerViewAdapter(recipeList); 

     recyclerView.setAdapter(simpleItemRecyclerViewAdapter); 

     if (findViewById(R.id.item_detail_container) != null) { 

      mTwoPane = true; 
     } 
0

尝试从您的Recipe.class

像所有的属性创建构造: 公共成分(字符串措施,弦乐成分,字符串量){ this.measure =措施; this.ingredients =成分; this.quantity =数量 }

在所有构成列表对象的类中做同样的事情。