onPostExecute ArrayList

时间:2017-10-12 20:05:07

标签: java android android-asynctask

我用它的适配器编写了这个代码用于recycleview的主要活动,并从json数据中获取数据,所以我的问题是关于onPostExecute() 我已经在字符串之前使用了所以我使用了.append并且一切正常,现在我第一次使用了一个arraylist所以我的功能是什么问题

主要活动:

2

适配器:

public class MainActivity extends AppCompatActivity {

    private static final ArrayList<MovieItem> NUM_MOVIES = ;
    RecyclerViewAdapter mAdapter;
    RecyclerView mMoviesList;

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

        mMoviesList = (RecyclerView) findViewById(R.id.rv_movies);

        GridLayoutManager LayoutManager = new GridLayoutManager(this, 2);

        mMoviesList.setLayoutManager(LayoutManager);

        mMoviesList.setHasFixedSize(true);

        mAdapter = new RecyclerViewAdapter(NUM_MOVIES);

        mMoviesList.setAdapter(mAdapter);

        loadMoviesData();
    }

    private void loadMoviesData() {
        new FetchMoviesTask().execute();
    }

    public class FetchMoviesTask extends AsyncTask<String, Void, ArrayList<MovieItem>> {

        @Override
        protected ArrayList<MovieItem> doInBackground(String... params) {
            if (params.length == 0) {
                return null;
            }
            String movie = params[0];
            URL moviesRequestUrl = NetworkUtils.buildUrl(movie);

            try {
                String jsonMovieResponse = NetworkUtils.getResponseFromHttpUrl(moviesRequestUrl);

                ArrayList<MovieItem> simpleJsonMovieData = OpenMovieJsonUtils.getSimpleMovieStringsFromJson(MainActivity.this, jsonMovieResponse);

                return simpleJsonMovieData;

            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        protected void onPostExecute(ArrayList<MovieItem> movieData) {
            if (movieData != null) {
                for (MovieItem movie : movieData) {
                    mMoviesList.append((movie) + "\n\n\n");
                }
            }
        }
    }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.sort, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();

            if (id == R.id.most_popular) {
                loadMoviesData();
                return true;
            }

            if (id == R.id.top_rated) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }

关于public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> { private static final String TAG = RecyclerViewAdapter.class.getSimpleName(); ArrayList<MovieItem> mMoviesItems; private Context context; public RecyclerViewAdapter(ArrayList<MovieItem> MoviesItems) { mMoviesItems = MoviesItems; } @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { context = viewGroup.getContext(); int layoutIdForListItem = R.layout.movie_list_item; LayoutInflater inflater = LayoutInflater.from(context); boolean shouldAttachToParentImmediately = false; View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately); RecyclerViewHolder viewHolder = new RecyclerViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(RecyclerViewHolder holder, int position) { holder.MoviePopularity.setText(String.valueOf(mMoviesItems.get(position).getPopularity())); Picasso.with(this.context).load(mMoviesItems.get(position).getPhoto()).into(holder.MoviePoster); holder.MovieName.setText(mMoviesItems.get(position).getName()); } @Override public int getItemCount() { return mMoviesItems.size(); } class RecyclerViewHolder extends RecyclerView.ViewHolder { TextView MoviePopularity; ImageView MoviePoster; TextView MovieName; public RecyclerViewHolder(View itemView) { super(itemView); MoviePopularity = (TextView)itemView.findViewById(R.id.movie_popularity); MoviePoster = (ImageView)itemView.findViewById(R.id.iv_item_movie); MovieName = (TextView)itemView.findViewById(R.id.movie_name); } } } 当我使用int时我只使用了任何数字但现在使用arraylist我应该做什么

1 个答案:

答案 0 :(得分:0)

这是因为您正在尝试使用该类不存在的方法。

在源代码中,您将mMoviesList定义为RecyclerView

RecyclerView mMoviesList;

然后你尝试以下方法:

protected void onPostExecute(ArrayList<MovieItem> movieData) {
  if (movieData != null) {
     for (MovieItem movie : movieData) {
         mMoviesList.append((movie) + "\n\n\n");
     }
  }
}

RecyclerView中没有append方法