Problems when deploying Glide with GridView on Android

1

I have a problem when implementing Glide in my GridView, based on this page Glide

  

activity_index.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/main.appbar"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true" >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        app:title="Bienvenido">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="30dp"
            android:lineSpacingExtra="8dp"
            android:id="@+id/data_checkin" />

        <RelativeLayout
            android:layout_gravity="bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_picture"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:src="@drawable/picture"
                android:tint="@color/colorPrimary"
                app:backgroundTint="#fff"
                app:borderWidth="0dp"
                app:fabSize="mini"
                app:layout_anchorGravity="bottom|end"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_toLeftOf="@+id/fab_home"
                android:layout_marginEnd="20dp" />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_marginLeft="16dp"
                android:src="@drawable/home"
                android:tint="@color/colorPrimary"
                app:backgroundTint="#fff"
                app:borderWidth="0dp"
                app:fabSize="mini"
                app:layout_anchorGravity="bottom|end"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="23dp"
                android:layout_marginBottom="29dp"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/main.toolbar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            app:popupTheme="@style/ThemeScot"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin">

        <GridView
            android:id="@+id/grid_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:columnWidth="300dp"
            android:drawSelectorOnTop="true"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp"
            android:focusable="true"
            android:clickable="true" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

  

IndexActivity.java

ArrayList<String> strings = new ArrayList<String>();
    if (checkIn.getImages().size() > 0) {//valido de que existan imagenes
        for (ImageData object: checkIn.getImages()) {
            strings.add(object.getPath());//al ArrayList le agrego los Bitmaps
        }//for
        gridView.setAdapter(new ImageAdapter(this, strings));//seteo las imagenes al gridView
    }//./if
  

ImageAdapter.java

public class ImageAdapter extends ArrayAdapter {

private Context mContext;
private ArrayList<String> imagesList;
private LayoutInflater inflater;

public ImageAdapter(Context context, ArrayList<String> imagesList){
    super(context, R.layout.activity_index, imagesList);//recibe como parametros el Contexto y un ArrayList de Bitmaps
    this.mContext = context;//se asgina valores
    this.imagesList = imagesList;//se asgina valores
    inflater = LayoutInflater.from(mContext);
}//./constructor

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (null == convertView) {
        convertView = inflater.inflate(R.layout.activity_index, parent, false);
    }

    Glide.with(mContext).load(imagesList.get(position)).into((ImageView) convertView);

    return convertView;
}

}//./clase

is marking me the following error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.tritonix.scotproyect, PID: 4923
              java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout cannot be cast to android.widget.ImageView
                  at com.example.tritonix.scotproyect.ImageAdapter.getView(ImageAdapter.java:65)
                  at android.widget.AbsListView.obtainView(AbsListView.java:2828)
                  at android.widget.GridView.onMeasure(GridView.java:1064)
                  at android.view.View.measure(View.java:18648)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                  at android.view.View.measure(View.java:18648)
                  at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1420)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                  at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:482)
                  at android.view.View.measure(View.java:18648)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
                  at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
                  at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                  at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367)
                  at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768)
                  at android.view.View.measure(View.java:18648)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                  at android.view.View.measure(View.java:18648)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                  at android.view.View.measure(View.java:18648)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3012)
                  at android.view.View.measure(View.java:18648)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2298)
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1345)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1644)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6748)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:590)
                  at android.view.Choreographer.doFrame(Choreographer.java:560)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:6134)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
    
asked by Javier fr 22.12.2016 в 20:11
source

2 answers

3

The reference of ImageView is missing where you will add the image, in fact it is not contained in the layout activity_index.xml :

  Glide.with(mContext).load(imagesList.get(position)).into(ImageView);

You have to get the reference of the ImageView container where the image would be loaded, for example:

ImageView myImageView = (ImageView) findViewById(R.id.imageView);

 Glide.with(mContext).load(imagesList.get(position)).into(myImageView);

Example implementing the loading of the image within getView() :

@Override public View getView(int position, View convertView, ViewGroup parent) { 
ImageView imageView = new ImageView(mContext); 
if (imagesList.size() > 0) { imageView.setImageBitmap(imagesList.get(position)); } 
//caracteristicas del ImageView imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(new GridView.LayoutParams(150, 150)); 
Glide.with(mContext).load(imagesList.get(position)).into(imageView);
return imageView; 
}
    
answered by 22.12.2016 / 20:15
source
3

Well I'll show you my method to use Glide and Gridview . first I create a class called Foto where it will caragara a name and the respective image (local)

public class Foto {
private String nombre;
private int idDrawable;

public Foto(String nombre, int idDrawable) {
    this.nombre = nombre;
    this.idDrawable = idDrawable;
}

public String getNombre() {
    return nombre;
}

public int getIdDrawable() {
    return idDrawable;
}

public int getId() {
    return nombre.hashCode();
}

public static Foto[] ITEMS = {
        new Foto("Ejemplo 1", R.drawable.sts),
        new Foto("Ejemplo 2", R.drawable.imagen),
        new Foto("Ejemplo 3", R.drawable.auditorio1),
        new Foto("Ejemplo 4", R.drawable.auditorio2),
        new Foto("Ejemplo 5", R.drawable.auditorio3),
        new Foto("Ejemplo 6", R.drawable.auditorio4),
        new Foto("Ejemplo 7",R.mipmap.st),
        new Foto("Ejemplo 8", R.mipmap.st),
        new Foto ("Ejemplo 9", R.mipmap.st),
        new Foto("Ejemplo 10", R.mipmap.st),
};

/**
 * Obtiene item basado en su identificador
 *
 * @param id identificador
 * 
 */
public static Foto getItem(int id) {
    for (Foto item : ITEMS) {
        if (item.getId() == id) {
            return item;
        }
    }
    return null;
}

}

Then I create a class called AdaptadorDeFotos this implements Glide to load the photos

public class AdaptadorDeFotos extends BaseAdapter {
private Context context;

public AdaptadorDeFotos(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return Foto.ITEMS.length;
}

@Override
public Foto getItem(int position) {
    return Foto.ITEMS[position];
}

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

@Override
public View getView(int position, View view, ViewGroup viewGroup) {

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.grid_item, viewGroup, false);
    }

    ImageView imagenCoche = (ImageView) view.findViewById(R.id.imagen_coche);
    TextView nombreCoche = (TextView) view.findViewById(R.id.nombre_coche);
    final Foto item = getItem(position);
    Glide.with(imagenCoche.getContext())
            .load(item.getIdDrawable())
            .into(imagenCoche);

    nombreCoche.setText(item.getNombre());

    return view;
}

}

and then in the main class

public class AdaptadorDeFotos extends BaseAdapter {
private Context context;

public AdaptadorDeFotos(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return Foto.ITEMS.length;
}

@Override
public Foto getItem(int position) {
    return Foto.ITEMS[position];
}

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

@Override
public View getView(int position, View view, ViewGroup viewGroup) {

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.grid_item, viewGroup, false);
    }

    ImageView imagenCoche = (ImageView) view.findViewById(R.id.imagen_coche);
    TextView nombreCoche = (TextView) view.findViewById(R.id.nombre_coche);
    final Foto item = getItem(position);
    Glide.with(imagenCoche.getContext())
            .load(item.getIdDrawable())
            .into(imagenCoche);

    nombreCoche.setText(item.getNombre());

    return view;
}

}

and for when the user clicks on the image it opens in a new activity

public class ActividadPrincipal extends AppCompatActivity implements AdapterView.OnItemClickListener

{
    private GridView gridView;
    private AdaptadorDeFotos adaptador;

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


    gridView = (GridView) findViewById(R.id.grid);
    adaptador = new AdaptadorDeFotos(this);
    gridView.setAdapter(adaptador);
    gridView.setOnItemClickListener(this);
}


    @Override
    public void onItemClick (AdapterView < ? > parent, View view,int position, long id){
    Foto item = (Foto) parent.getItemAtPosition(position);

    Intent intent = new Intent(this, ActividadDetalle.class);
    intent.putExtra(ActividadDetalle.EXTRA_PARAM_ID, item.getId());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        ActivityOptionsCompat activityOptions =
                ActivityOptionsCompat.makeSceneTransitionAnimation(
                        this,
                        new Pair<View, String>(view.findViewById(R.id.imagen_coche),
                                ActividadDetalle.VIEW_NAME_HEADER_IMAGE)
                );

        ActivityCompat.startActivity(this, intent, activityOptions.toBundle());
    } else
        startActivity(intent);
}

XML main activity

<?xml version="1.0" encoding="utf-8"?>

<GridView
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="150dp"
    android:horizontalSpacing="1dp"
    android:numColumns="auto_fit"
    android:padding="0dp"
    android:verticalSpacing="1dp" />

and the layout when clicking on the image

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_actividad_detalle"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.example.hp14.iteracion2.Galeria.ActividadDetalle"
android:background="@color/black_overlay">

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imagen_extendida"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

</ImageView>

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:theme="?attr/actionBarTheme"
    android:minHeight="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/toolbar3" />

I hope I will help you in something, greetings!

    
answered by 22.12.2016 в 21:11