I have seen several other questions regarding GetView being called in an adapter several times and causing performance issues. However, this question is not a duplicate.
All of the answers I have found mention that having a ListView with a height of wrap_content will cause such behavior, but I am using an adapter to populate a GridView, with the height set to match_parent. GetView is called over and over until the activity crashes.
Here is the layout with the GridView I am trying to populate:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.jeff_fennell.capstone.GroupDetailView"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/members_in_group"
android:textIsSelectable="false"
android:textSize="25sp"
android:id="@+id/group_detail_name"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/invite_member_button"
android:text="@string/invite_friends_button"
android:onClick="openInviteDialog"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/group_empty_message"
android:visibility="invisible"
android:text="@string/group_empty_message"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"/>
<it.sephiroth.android.library.widget.HListView
android:layout_width="match_parent"
android:layout_height="90dp"
android:id="@+id/members_in_group"
android:layout_weight="0">
</it.sephiroth.android.library.widget.HListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/albums"
android:textIsSelectable="false"
android:textSize="25sp"
android:id="@+id/group_albums"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/new_album_button"
android:text="@string/new_album_button_text"
android:onClick="openCreateAlbumDialog"/>
</LinearLayout>
</LinearLayout>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:id="@+id/album_list">
</GridView>
</LinearLayout>
This is the adapter:
public class AlbumAdapter extends ArrayAdapter<Album>{
private Activity activity;
public AlbumAdapter(Activity activity, List<Album> albums) {
super(getApplicationContext(), R.layout.album_preview, albums);
this.activity = activity;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Album album = getItem(position);
RelativeLayout albumView = null;
if (convertView != null) {
albumView = (RelativeLayout)convertView;
} else {
albumView = (RelativeLayout)getLayoutInflater().inflate(R.layout.album_preview, null);
}
TextView albumName = (TextView)albumView.findViewById(R.id.album_name);
albumName.setText(album.getName());
Call<List<Image>> getAlbumPreview = Utils.getClient()
.getAlbumPreviewImages(
album.getGroupId(),
album.getAlbumId(),
UserProfile.getAuthenticationToken(activity)
);
getAlbumPreview.enqueue(new Callback<List<Image>>() {
@Override
public void onResponse(Call<List<Image>> call, Response<List<Image>> response) {
if (response.isSuccessful()) {
List<Image> previewImages = response.body();
for (int i = 0; i < previewImages.size(); i++) {
Image image = previewImages.get(i);
int[] imageViewsToPopulate = {
R.id.image_1,
R.id.image_2,
R.id.image_3,
R.id.image_4,
R.id.image_5
};
final ImageView previewImageView = (ImageView)findViewById(imageViewsToPopulate[i]);
Glide
.with(activity)
.load(image.getUrl())
.asBitmap()
.placeholder(R.drawable.ic_people_white_48dp)
.centerCrop()
.into(new BitmapImageViewTarget(previewImageView) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(activity.getResources(), resource);
circularBitmapDrawable.setCircular(true);
previewImageView.setImageDrawable(circularBitmapDrawable);
}
});
}
} else {
//set no images message
}
}
@Override
public void onFailure(Call<List<Image>> call, Throwable t) {
}
});
return albumView;
}
I realize I have some hard-coded values, and the code needs to be cleaned up a little, but I just haven't gotten around to cleanup/refactoring yet.
Any help would be appreciated. Thanks.