Rellenar GridView con arraylist
Publicado por Angel (5 intervenciones) el 24/11/2016 13:00:59
Tengo un problema al hacer uso de GridView ya que quiero añadir los elementos a través de un arraylist de tipo ElementoGrid y no hay manera. Aquí os dejo el código para ver si veis el error.
__________________________________________________________________________________
CLASES JAVA
------------------------------------------------------------------
---------------------------------------------------------------------------------------
RECURSOS .XML
formato_item.xml
------------------------------------------------------------------------------------------
grid_layout.xml
__________________________________________________________________________________
CLASES JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ElementoGrid {
private int idItem;
private int idImagen;
public ElementoGrid(int item, int imagen){
this.idItem=item;
this.idImagen=imagen;
}
public int getIdItem(){
return idItem;
}
public int getIdImagen(){
return idImagen;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class GridActivity extends Activity {
GridView gridView;
GridAdapter gridAdapter;
ArrayList<ElementoGrid> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
// *** referencia los layout
gridView = (GridView) findViewById(R.id.gridview);
// creamos nuestra coleccion de datos
items = new ArrayList<ElementoGrid>();
items.add(new ElementoGrid(1,R.drawable.emoticono1));
items.add(new ElementoGrid(2,R.drawable.emoticono1));
items.add(new ElementoGrid(3,R.drawable.emoticono1));
items.add(new ElementoGrid(4,R.drawable.emoticono1));
items.add(new ElementoGrid(5,R.drawable.emoticono1));
// creamos el listado
gridAdapter = new GridAdapter(GridActivity.this, items);
// establecemos el adaptador en la lista
gridView.setAdapter(gridAdapter);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class GridAdapter extends ArrayAdapter<ElementoGrid> {
private Activity activity;
ImageView imagV;
ArrayList<ElementoGrid> items;
public GridAdapter(Activity activity, ArrayList<ElementoGrid> items){
super(activity, R.layout.formato_item);
this.activity = activity;
this.items = items;
}
static class ViewHolder {
protected TextView idIte;
protected ImageView imageIcon;
}
public int getCount() {
return items.size();
}
public long getItemPosition(int position) {
return position;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
View view = null;
LayoutInflater inflator = activity.getLayoutInflater();
view = inflator.inflate(R.layout.formato_item, null);
//INSTANCIANDO RECURSOS
final ViewHolder viewHolder = new ViewHolder();
viewHolder.idIte = (TextView) view.findViewById(R.id.idItem);
viewHolder.imageIcon = (ImageView) view.findViewById(R.id.imageIcon);
viewHolder.idIte.setText(items.get(position).getIdItem());
viewHolder.imageIcon.setImageResource(items.get(position).getIdImagen());
return view;
}
}
RECURSOS .XML
formato_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/idItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"/>
<ImageView
android:id="@+id/imageIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@android:drawable/ic_menu_gallery" />
</LinearLayout>
grid_layout.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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"
tools:context=".GridActivity">
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Valora esta pregunta


0