没有附加适配器;跳绳布局:RecyclerView

问题描述:

MainActivity.class没有附加适配器;跳绳布局:RecyclerView

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) { 

      } 
     }); 

    } 

} 

错误

2月5日至13日: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:没有附加适配器;跳过布局

在此先感谢!

擦除方法loadData()在Main/UI线程中,因为它是ASYNCHRONOUS在改进2,因此您不需要在主线程中执行此操作。

参考改装 - >https://futurestud.io/blog/retrofit-synchronous-and-asynchronous-requests

notifyDataSetChanged()适配器的方法onResponse()你填写你的结果到您的列表后。

movies.addAll(response.body().getResults()); moviesAdapter.notifyDataSetChanged();