Foros del Web » Programando para Internet » Android »

[SOLUCIONADO] Problema con ArrayAdapter

Estas en el tema de Problema con ArrayAdapter en el foro de Android en Foros del Web. Buenos días, tengo un problema con el ArrayAdapter, estoy haciendo que muestre texto y imagenes, pero al ejecutar la aplicación me muestra que tuvo que ...
  #1 (permalink)  
Antiguo 11/11/2014, 07:29
Avatar de guzzano  
Fecha de Ingreso: julio-2010
Ubicación: Isla de Margarita
Mensajes: 162
Antigüedad: 13 años, 8 meses
Puntos: 13
Problema con ArrayAdapter

Buenos días, tengo un problema con el ArrayAdapter, estoy haciendo que muestre texto y imagenes, pero al ejecutar la aplicación me muestra que tuvo que cerrarse.

La clase del Adapter
Código java:
Ver original
  1. package com.xxx;
  2.  
  3. import android.app.Activity;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10.  
  11. public class AdapterServices extends ArrayAdapter <String>
  12. {
  13.     private final Activity   Ct;
  14.     private final String[]   Text;
  15.     private final int[]      Img;
  16.     private final int        res;
  17.    
  18.     public AdapterServices (Activity c, String[] d, int[] i, int s)
  19.     {
  20.         super(c, s, d); /* */
  21.        
  22.         this.Ct  = c;
  23.         this.Text = d; /* El tamaño de Text, debería */
  24.         this.Img  = i; /* ser de igual al de Img. */
  25.         this.res  = s;
  26.     }
  27.    
  28.     @Override
  29.     public View getView (int position, View convertView, ViewGroup parent)
  30.     {
  31.         View v;
  32.        
  33.         if ((v = convertView) == null)
  34.         {
  35.             LayoutInflater i = (LayoutInflater) Ct
  36.                 .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  37.            
  38.             v = i.inflate(this.res, null);
  39.         }
  40.        
  41.         TextView t = (TextView) v.findViewById(R.id.ImageServicesListView);
  42.         t.setText(this.Text[position]);
  43.        
  44.         ImageView i = (ImageView) v.findViewById(R.id.TextServicesListView);
  45.         i.setImageResource(this.Img[position]);
  46.        
  47.         return v;
  48.     }
  49. }

Main

Código Java:
Ver original
  1. package com.xxx;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.ListView;
  6.  
  7. public class MainActivity extends Activity
  8. {
  9.     @Override
  10.     public void onCreate(Bundle savedInstanceState)
  11.     {  
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.        
  15.         String[] s = new String[] { "Probando 1", "Probando 2" };
  16.         int[]    i = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher };
  17.        
  18.         ListView l = (ListView) findViewById(R.id.list_services);
  19.         AdapterServices a = new AdapterServices(this, s, i, R.layout.serviceslistview);
  20.        
  21.         l.setAdapter(a);
  22.     }
  23. }

Y los respectivos XML que meteré en el ListView

Código XML:
Ver original
  1. <?xml version = "1.0" encoding = "UTF-8" ?>
  2.  
  3. <LinearLayout  
  4.    xmlns:android = "http://schemas.android.com/apk/res/android"
  5.    
  6.    android:orientation = "vertical"            
  7.    android:layout_width = "fill_parent"
  8.    android:layout_height = "fill_parent"
  9.    
  10.    android:background = "#5ba3d5"
  11. >
  12.  
  13.     <ImageView
  14.        android:id = "@+id/ImageServicesListView"
  15.        
  16.        android:layout_width = "60dp"
  17.        android:layout_height = "60dp"    />
  18.        
  19.     <TextView
  20.        android:id = "@+id/TextServicesListView"
  21.        
  22.        android:layout_width = "60dp"
  23.        android:layout_height = "60dp"    />
  24.    
  25. </LinearLayout>

Y el XML del main

Código XML:
Ver original
  1. <?xml version = "1.0" encoding = "utf-8" ?>
  2.  
  3. <LinearLayout  
  4.    xmlns:android = "http://schemas.android.com/apk/res/android"
  5.    
  6.    android:orientation = "vertical"            
  7.    android:layout_width = "fill_parent"
  8.    android:layout_height = "fill_parent"
  9.    
  10.    android:background = "#5ba3d5"
  11. >
  12.    
  13.     <ListView
  14.        android:id = "@+id/list_services"
  15.        
  16.        android:layout_width = "fill_parent"
  17.        android:layout_height = "fill_parent" />
  18. </LinearLayout>

Espero que me puedan ayudar, un saludos, muchas gracias.

---

Resolví el problema, estaba aquí.

Código Java:
Ver original
  1. TextView t = (TextView) v.findViewById(R.id.ImageServicesListView);
  2.         t.setText(this.Text[position]);
  3.        
  4.         ImageView i = (ImageView) v.findViewById(R.id.TextServicesListView);
  5.         i.setImageResource(this.Img[position]);

Estaba usando el ID del TextView en el ImageView y viceversa. Muchas gracias igual.
__________________
Si me equivoco, corríjanme sin piedad.

Última edición por guzzano; 11/11/2014 a las 09:41

Etiquetas: androi, list
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:11.