MainActivity.class
public class MainActivity extends AppCompatActivity {
private static final String TAG = "ERROR";
private final static String API_KEY = "xxxxxxxxxxxxxx";
int totalpage;
int page;
int firstVisibleItem, visibleItemCount, totalItemCount;
ProgressBar progressBar;
RecyclerView recyclerView;
MoviesAdapter moviesAdapter;
private boolean makeCall = false;
boolean onLoding = false;
List<Movie> movies = new ArrayList<Movie>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
loadData();
moviesAdapter = new MoviesAdapter(movies, R.layout.list_item_movie, MainActivity.this);
moviesAdapter.notifyDataSetChanged();
recyclerView.setAdapter(moviesAdapter);
}
public void loadData() {
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<String> call = apiService.getTopRatedMovies(API_KEY);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.i("RetrofitOnly", response.body());
String responceString = response.body();
JSONObject main;
try {
main = new JSONObject(responceString);
page = main.getInt("page");
totalpage = main.getInt("total_pages");
Log.d("PageNo==>>", page + "");
JSONArray jsonArray = main.getJSONArray("results");
try {
Log.d("Array==>>", jsonArray.toString() + "");
movies = LoganSquare.parseList(jsonArray.toString(), Movie.class);
if (movies == null) {
Toast.makeText(getApplicationContext(), "NULL", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "NULL NOT", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
}
错误
05-13 02:25:15.145 23598-23598 / com.example.dhaval.retrofitonly E / RecyclerView:没有连接适配器;跳过布局
05-13 02:25:15.481 23598-23598 / com.example.dhaval.retrofitonly E / RecyclerView:没有连接适配器;跳过布局
提前致谢!!!
答案 0 :(得分:1)
在主要/用户界面线程中删除方法loadData()
,因为它在改进2中为ASYNCHRONOUS
,因此您不需要在主线程中执行此操作。
参考改造 - &gt; https://futurestud.io/blog/retrofit-synchronous-and-asynchronous-requests
将结果填入列表后,和notifyDataSetChanged()
方法onResponse()
中的适配器。
movies.addAll(response.body().getResults());
moviesAdapter.notifyDataSetChanged();