我尝试使用翻新和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();
}
});
}
} 我尝试了很多方法但还是一样 谢谢!
谢谢您的回答,已经解决了。
答案 0 :(得分:0)
在设置适配器之前将其删除
adapter.notifyDataSetChanged();
答案 1 :(得分:0)
import org.openqa.selenium.remote.RemoteWebDriver;
import java.util.logging.Level;
执行此操作
You have to set adapter first and then notify it.
代替-:
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
答案 2 :(得分:0)
尝试这样
private List<Movie> movie; // better initialize it inside onCreate.
//...
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
movie = new ArrayList<>(); // here you should initialize movie.
// here initialize your adapter with empty movie List.
adapter = new MovieAdapter(movie,this);
// set adapter to your recyclerview
recyclerView.setAdapter(adapter);
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();
if(response.body() != null){
movie.clear();
movie.addAll(response.body());
adapter.notifyDataSetChanged(); // now notify your adapter that data set has changed
}
}
@Override
public void onFailure(Call<List<Movie>> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_LONG).show();
}
});
答案 3 :(得分:0)
请添加此行
recyclerView.setLayoutManager(new LinearLayoutManager(this));
在设置适配器后,如果它不能正常工作,则还要检查您的xml文件。