无法在Android中填充RecyclerView

时间:2016-10-15 21:04:57

标签: android listview android-recyclerview android-adapter recycler-adapter

完整代码链接:https://github.com/sudhishkr/Flicks/tree/feature/stackoverflow

我正在尝试构建一个电影信息显示应用。我正在尝试使用recyclerview,但似乎在网络调用后没有加载我的数据。

我的主要活动:

public class MainActivity extends AppCompatActivity {

    TextView textView;
    RecyclerView recyclerViewTheMovieDB;
    RecyclerViewAdapter recyclerViewAdapterTheMovieDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerViewTheMovieDB = (RecyclerView) findViewById(R.id.recyclerViewTheMovieDB);
        recyclerViewAdapterTheMovieDB = new RecyclerViewAdapter(getApplicationContext());
        recyclerViewTheMovieDB.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        recyclerViewTheMovieDB.setAdapter(recyclerViewAdapterTheMovieDB);
        textView = (TextView) findViewById(R.id.test2);
        VolleyCallback callback = new VolleyCallback() {
            @Override
            public void onSuccessResponse(ArrayList<Movie> result) {
                try {
                    recyclerViewAdapterTheMovieDB.setMovieList(result);
                    textView.setText(result.get(0).getTitle());
                } catch (Exception e){

                }
                return;
            }
        };
        final RequestQueue queue = Volley.newRequestQueue(this);
        TheMovieDBApi theMovieDBApiObj = new TheMovieDBApi();
        theMovieDBApiObj.getMovieList(queue, getApplicationContext(), callback);

    }
}

我的RecyclerViewAdapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolderTheMovieDB>{


    private ArrayList<Movie> listMovies = new ArrayList<>();
    private LayoutInflater layoutInflater;

    public void setMovieList(ArrayList<Movie> listMovies){
        this.listMovies = listMovies;
        notifyDataSetChanged();
        notifyItemRangeChanged(0, listMovies.size());
    }

    public RecyclerViewAdapter(Context context){
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolderTheMovieDB onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.layout_inflater_themoviedb, parent, false);
        ViewHolderTheMovieDB viewHolderTheMovieDB = new ViewHolderTheMovieDB(view);
        return viewHolderTheMovieDB;
    }

    @Override
    public void onBindViewHolder(ViewHolderTheMovieDB holder, int position) {
        Movie currentMovie = listMovies.get(position);
        try {
            holder.title.setText(currentMovie.getTitle());
            holder.overview.setText(currentMovie.getOverview());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return listMovies.size();
    }

    static class ViewHolderTheMovieDB extends RecyclerView.ViewHolder{

        private TextView title;
        private TextView overview;

        public ViewHolderTheMovieDB(View itemView){
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.textViewTitle);
            overview = (TextView) itemView.findViewById(R.id.textViewOverview);
        }
    }
}

当我运行此代码时,notifyDataSetChangednotifyItemRangeChanged似乎无法填充回收站视图。

我也在设置textview第一项,这似乎有效。我正在努力弄清楚RecyclerView出了什么问题。任何帮助/方向都会很棒。

0 个答案:

没有答案