Reputation: 1858
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
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;
}
}
LAYOUT
<?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>
IMPLEMENTATION
public class MainActivity extends ActionBarActivity {
private Spinner spinnerMovieSelection;
private GridView gridViewMovie;
private MovieAdapter movieAdapter;
SortByAdapter sortByAdapter;
private List<Movie> finalMovieList;
private ImageView ivPoweredBy;
private ArrayList<String> sortByList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
finalMovieList = new ArrayList<Movie>();
sortByList = new ArrayList<String>();
populateSortByList();
gridViewMovie = (GridView) findViewById(R.id.gridViewMovie);
spinnerMovieSelection = (Spinner) findViewById(R.id.spinnerMovieSelection);
ivPoweredBy = (ImageView) findViewById(R.id.ivPoweredBy);
sortByAdapter = new SortByAdapter(getApplicationContext(), sortByList);
movieAdapter = new MovieAdapter(getApplicationContext(), finalMovieList);
spinnerMovieSelection.setAdapter(sortByAdapter);
gridViewMovie.setAdapter(movieAdapter);
spinnerMovieSelection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
getMovies((String) parent.getItemAtPosition(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void populateSortByList() {
sortByList.add("Popularity");
sortByList.add("Highest Rated");
}
private void getMovies(String sortBy) {
Log.e("RANDOM TAG", "GET MOVIES CALLED");
MovieAsyncTask movieAsyncTask = new MovieAsyncTask() {
@Override
public void onResponseReceived(List<Movie> movieList) {
finalMovieList = movieList;
movieAdapter.notifyDataSetChanged();
}
};
movieAsyncTask.execute(sortBy);
}
}
Upvotes: 0
Views: 460
Reputation: 3054
The point here is the list in your Movie adapter always empty.
You initial the movie adapter with a empty list.
Then in onResponseReceived
in getMovies
, you assign finalMovieList = movieList;
then notifyDatasetChanged
, But the adapter still empty because the finalMoviewList
point to another reference now
Try this:
finalMovieList.clear();
if (movieList != null) {
finalMovieList.addAll(movieList);
}
movieAdapter.notifyDataSetChanged();
Hope this helps.
Upvotes: 1