Images not showing up in GridView

时间:2015-07-29 00:19:10

标签: android gridview imageview android-arrayadapter

I cannot seem to figure out why my ImageView does not get populated in a GridView which has a Custom ArrayAdapter class attached to it.

I was trying to populate the images based on an AsyncTask call, but I decided to just try hard coding a drawable into the ImageView within the layout being used, and it still does not show anything.

Is there anything that stands out with my implementation?

I am not receiving any errors, and one thing I noticed is when I try to debug it does not hit my breakpoint within getView().

ADAPTER CLASS


undefined
undefined
undefined
undefined
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33

LAYOUT

public class MovieAdapter extends ArrayAdapter<Movie> {

    private LayoutInflater mInflater;

    public MovieAdapter(Context context, List<Movie> movies) {
        super(context, 0, movies);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        Movie movie = getItem(position);
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.movie_item, parent, false);
            vh = new ViewHolder();
            vh.ivPoster = (ImageView) convertView.findViewById(R.id.ivMoviePoster);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        vh.ivPoster.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ant_man));

        //Picasso.with(getContext()).load(movie.getFull_poster_path()).into(vh.ivPoster);
        return convertView;
    }

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

    @Override
    public int getPosition(Movie item) {
        return super.getPosition(item);
    }

    private class ViewHolder {
        private ImageView ivPoster;

    }
}

IMPLEMENTATION

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent" android:orientation="vertical">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ant_man"
            android:id="@+id/ivMoviePoster"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

这里的要点是电影适配器中的列表始终为空。
您使用空列表初始化电影适配器 然后在getMovies的{​​{1}}中,您分配finalMovieList = movieList;然后notifyDatasetChanged但是适配器仍然为空,因为finalMoviewList现在指向另一个引用
试试这个:

finalMovieList.clear();
if (movieList != null) {
  finalMovieList.addAll(movieList);
} 
movieAdapter.notifyDataSetChanged();

希望这有帮助。