我尝试使用改造和recyclerview从API获取数据
但是跑步说
E / RecyclerView:未连接适配器;跳过布局
MainActivity.class
public class MainActivity extends AppCompatActivity {
private MovieAdapter adapter;
private RecyclerView recyclerView;
private String API_KEY = ApiClient.getApiKey();
private String CATEGORY = "now_playing";
private String LANGUAGE = "en-US";
private List<Movie> movie = new ArrayList<>();
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading .... ");
progressDialog.show();
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
GetMovie service = ApiClient.getRetrofitInstance().create(GetMovie.class);
Call<List<Movie>> call = service.getMovie(CATEGORY,API_KEY,LANGUAGE);
call.enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
progressDialog.dismiss();
movie = response.body();
adapter = new MovieAdapter(movie,getApplicationContext());
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<List<Movie>> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_LONG).show();
}
});
}
}我尝试了很多方法但还是一样谢谢!
You have to set adapter first and then notify it.
执行此操作
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
而不是-:
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
在设置适配器之前将其删除
adapter.notifyDataSetChanged();