我实现了回收站视图,我使用改造来从api获取数据
数据作为数组json
之后,我实现改造对象,在for循环内我有问题我无法获取数据发送模型的模型,我没有任何错误消息,只是空白屏幕
和logat中的这条消息
E / RecyclerView:没有连接适配器;跳过布局
这是我的主要活动:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private LinearLayoutManager mlinearLayout;
ArrayList<Model> list;
Adapter adapter;
String url = "https://d17h27t6h515a5.cloudfront.net/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
mlinearLayout = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(mlinearLayout);
list = new ArrayList<>();
getRetrofitArray();
adapter=new Adapter(list ,MainActivity.this);
recyclerView.setAdapter(adapter);
}
public void getRetrofitArray() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitArrayAPI servese =retrofit.create(RetrofitArrayAPI.class);
Call< List<Recipe>> call=servese.getRecipeDetails();
call.enqueue(new Callback<List<Recipe>>() {
@Override
public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) {
for(int i=0;i<response.body().size();i++){
list.add(new Model(response.body().get(i).getName(),response.body().get(i).getServings(),Model.Image));
}
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<Recipe>> call, Throwable t) {
Log.e("main","retrifit error"+ t.toString());
}
});
}
我的模特
答案 0 :(得分:0)
尝试一次:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private LinearLayoutManager mlinearLayout;
ArrayList<Model> list;
Adapter adapter;
String url = "https://d17h27t6h515a5.cloudfront.net/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
list = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
mlinearLayout = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(mlinearLayout);
adapter=new Adapter(list ,MainActivity.this);
recyclerView.setAdapter(adapter);
getRetrofitArray();
}
public void getRetrofitArray() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitArrayAPI servese =retrofit.create(RetrofitArrayAPI.class);
Call< List<Recipe>> call=servese.getRecipeDetails();
call.enqueue(new Callback<List<Recipe>>() {
@Override
public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) {
list=response.body();
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<Recipe>> call, Throwable t) {
Log.e("main","retrifit error"+ t.toString());
}
});
}
请告诉我这是否适合您。
答案 1 :(得分:-1)
如果要更改适配器中的数据,需要使用适配器中的数据的setter方法更改它,然后调用notifyDataSetChanged()
例如:
list = getNewData();
adapter.setData(list); //need to set every time that data was changed
adapter.notifyDataSetChanged();