【问题标题】:Android how to use picasso with GridView?Android 如何在 GridView 中使用毕加索?
【发布时间】:2015-11-01 05:51:04
【问题描述】:

我正在尝试使用 Picasso 库,以便使用 AsyncTask 将 TMDB API 中的电影海报加载到网格视图中,因此我使用了自定义适配器,但没有任何反应,因此我尝试加载单个硬编码图像,但它也没有加载。我在自定义适配器的“getView”方法中放置了一个断点,并注意到它永远不会触发...... 那么,我在这里做错了什么?

这是我的自定义适配器:

public class MoviesAdapter extends ArrayAdapter<MovieEntity> {
    private Context context;
    private List<MovieEntity> movies = new ArrayList<>();

    public MoviesAdapter(Context context, int resource, int textViewResourceId, List<MovieEntity> objects) {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
    }

    @Override
    public int getCount() {
        return movies.size();
    }

    @Override
    public MovieEntity getItem(int position) {
        return movies.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView view = (ImageView) convertView;
        if (view == null) {
            view = new ImageView(context);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }

        MovieEntity movieEntity = getItem(position);

        Picasso.with(context)
                .load(movieEntity.moviePoster)
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.error)
                .fit()
                .tag(context)
                .into(view);

        return view;
    }
}

这是我在“onCreateView”方法中的代码:

mMoviesAdapter = new MoviesAdapter(getActivity(),
                R.layout.grid_view_image_item,
                R.id.img_movie_poster,
                new ArrayList<MovieEntity>());

        GridView gridView = (GridView) rootVoew.findViewById(R.id.grid_movies);
        gridView.setAdapter(mMoviesAdapter);

在 AsyncTask 的 onPostExecute 方法中设置数据,如下所示:

protected void onPostExecute(String[] strings) {
            if (strings != null) {
                mMoviesAdapter.clear();
                mMoviesAdapter.addAll(strings);
            }
        }

我的网格视图中没有图像,我做错了什么?

【问题讨论】:

    标签: android gridview android-asynctask custom-adapter


    【解决方案1】:

    我假设mForecastList 是数组列表,而您的mMoviesAdaptermForecastList 的适配器。

    在你的 onCreateView 中

      mForecastList = new ArrayList<MovieEntity>();
      mMoviesAdapter = new MoviesAdapter(getActivity(),
                R.layout.grid_view_image_item,
                R.id.img_movie_poster, mForecastList);
    
        GridView gridView = (GridView) rootVoew.findViewById(R.id.grid_movies);
        gridView.setAdapter(mMoviesAdapter);
    

    在您的 postExecute 方法中。这样做..

      protected void onPostExecute(String[] strings) {
            if (strings != null) {
                if(mForecastList != null) {
                  mForecastList.clear();
                }
                // If `strings` is json data parse it to ArrayList<MovieEntity>
            //    mForecastList.addAll(stringsList);
                // Call notifyDataSetChanged() to refresh the data in the grid..
                mMoviesAdapter.notifyDataSetChanged();
            }
        }
    

    【讨论】:

    • 对不起,我不小心从另一个项目复制了,它的代码相同但名称不同。我将其固定为“mMoviesAdapter”的正确名称。根据文档,除非您将 'mNotifyOnChange' 指定为 false,否则方法 'addAll' 默认调用 'notifyDataSetChanged'。
    • 不,我只是在我的问题中把它复制错了......我猜是因为getCount 方法返回0,但是为什么???
    • 我希望它是来自 onPostExcute() 参数的 json 字符串。所以你必须将该json传递给 ArrayList
    • 这不是getCount方法,它是关于你正在解析的数据。如果你不能解析数据,那么它就不会存储在arraylist中......
    • 我有一个将字符串解析成json数组的方法,数据有效,没有问题。我相信问题出在getCount 方法中,但我不知道它返回 0...
    【解决方案2】:

    我不知道这是否是最佳解决方案,但它适用于我的情况。 我刚刚删除了

    @Override
    public int getCount() {
        return movies.size();
    }
    

    来自自定义适配器,它可能有自己的getCount...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 2017-10-13
      • 2018-01-15
      • 1970-01-01
      • 2017-05-09
      • 2020-08-30
      • 2019-05-26
      相关资源
      最近更新 更多