<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mobile development &#187; Image</title>
	<atom:link href="http://mobile.davidocs.com/tag/image/feed/" rel="self" type="application/rss+xml" />
	<link>http://mobile.davidocs.com</link>
	<description>Building Android and iPhone applications from scratch</description>
	<lastBuildDate>Wed, 30 Sep 2009 08:54:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
<image>
<link>http://mobile.davidocs.com</link>
<url>http://mobile.davidocs.com/wp-content/mbp-favicon/android-wallpaper4_1024x768.png</url>
<title>mobile development</title>
</image>
		<item>
		<title>Cómo añadir iconos a un ListView en Android</title>
		<link>http://mobile.davidocs.com/android/como-anadir-iconos-a-un-listview-en-android/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=como-anadir-iconos-a-un-listview-en-android</link>
		<comments>http://mobile.davidocs.com/android/como-anadir-iconos-a-un-listview-en-android/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 21:40:43 +0000</pubDate>
		<dc:creator>David González</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Tutoriales]]></category>
		<category><![CDATA[Icon]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[ListView]]></category>

		<guid isPermaLink="false">http://www.tech-xperience.com/mobiledev/?p=139</guid>
		<description><![CDATA[Hace poco añadimos un enlace a una gran cantidad de imágenes e iconos para incluir en los Proyectos de Android. Pues bien, ya va siendo hora de darles utilidad. Vamos a añadir un icono a un ListView, de ésta manera tendremos un diseño mucho más agradable e añadiremos alguna funcionalidad extra. [tweetmeme] ¿Qué es esto? [...]


Entradas relacionadas:<ol><li><a href='http://mobile.davidocs.com/android/como-instalar-el-teclado-del-htc-hero-en-el-htc-magic/' rel='bookmark' title='Permanent Link: Cómo instalar el teclado del HTC Hero en el HTC Magic'>Cómo instalar el teclado del HTC Hero en el HTC Magic</a></li>
<li><a href='http://mobile.davidocs.com/android/tratamiento-de-ficheros-xml-con-android/' rel='bookmark' title='Permanent Link: Tratamiento de ficheros XML con Android'>Tratamiento de ficheros XML con Android</a></li>
<li><a href='http://mobile.davidocs.com/android/coleccion-de-iconos-e-imagenes-para-android/' rel='bookmark' title='Permanent Link: Colección de iconos e imágenes para Android'>Colección de iconos e imágenes para Android</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p class="dropcap-first">Hace poco añadimos un <a href="http://mobile.davidocs.com/android/coleccion-de-iconos-e-imagenes-para-android/" target="_blank">enlace</a> a una gran cantidad de imágenes e iconos para incluir en los Proyectos de <em>Android</em>. Pues bien, ya va siendo hora de darles utilidad. Vamos a añadir un icono a un <em><a href="http://developer.android.com/reference/android/widget/ListView.html" target="_blank">ListView</a></em>, de ésta manera tendremos un diseño mucho más agradable e añadiremos alguna funcionalidad extra. <span style="color: #008000;"><span style="color: #000000;">[tweetmeme]</span></span></p>
<p><span style="color: #008000;"><strong><span style="font-size: medium;">¿Qué es esto?</span></strong></span></p>
<p>En éste tutorial vamos a añadir un icono a cada línea de un <em>ListView.</em></p>
<p><span style="font-size: medium;"><span style="color: #008000;"><strong>¿Qué voy a aprender?</strong></span></span></p>
<ul>
<li>Acceder a las propiedades de un control desde código.</li>
<li>Caputarar los eventos de un <em>ListView</em>.</li>
<li>Crear nuevas clases y utilizarlas en la aplicación.</li>
</ul>
<p><strong><span style="font-size: medium;"><span style="color: #008000;">¿Cómo quedará?</span></span></strong></p>
<p style="text-align: center;"><strong><span style="font-size: medium;"><span style="color: #008000;"><img class="size-full wp-image-147 aligncenter" title="screenshot1" src="http://www.tech-xperience.com/mobiledev/wp-content/uploads/2009/08/screenshot1.png" alt="screenshot1" width="325" height="585" /></span></span></strong></p>
<p style="text-align: left;"><strong><span style="font-size: medium;"><span style="color: #008000;"><span id="more-139"></span><br />
</span></span></strong></p>
<p><strong><span style="font-size: medium;"><span style="color: #008000;">Pasos a seguir:</span></span></strong></p>
<p>Para simplificar el tutorial crearemos un nuevo Proyecto de <em>Android </em>que creará el <em>ListView </em>y lo poblará con iconos y textos. De ésta manera será mucho más fácil <strong>añadirlo a otros proyectos</strong> sin invertir mucho tiempo en la tarea.</p>
<p>Lo primero que vamos a hacer es crear el diseño de nuestra aplicación. Se trata de un XML muy simple donde añadiremos un <em>ListView</em>.<strong><span style="font-size: medium;"><span style="color: #008000;"><br />
</span></span></strong></p>
<p><strong><span style="font-size: medium;"><span style="color: #008000;">main.xml:</span></span></strong></p>
<pre class="brush: xml; title: ; notranslate">

&lt; ?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

&lt;linearlayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:id=&quot;@+id/settings&quot;
android:orientation=&quot;vertical&quot;
android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;fill_parent&quot;&gt;

&lt;listview android:id=&quot;@+id/android:list&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
/&gt;

&lt;/linearlayout&gt;
</pre>
<p>Un <em>ListView</em>, por defecto, sólo permite añadir líneas de texto. Cabe destacar que si el número de elementos es mayor del que podemos ver en pantalla él solito se encarga de hacer un Scroll cuando movamos naveguemos por la pantalla, así que no hace falta crear un <a href="http://developer.android.com/reference/android/widget/Scroller.html" target="_blank">Scroller</a>,ya que el control es automático. Para poder añadir un icono vamos a tener que crear un nuevo Layout que corresponderá a la nueva línea que veremos en el ListView.</p>
<p><strong><span style="font-size: medium;"><span style="color: #008000;">IconRow.xml:</span></span></strong></p>
<pre class="brush: xml; title: ; notranslate">

&lt;relativelayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;?android:attr/listPreferredItemHeight&quot;
android:padding=&quot;6dip&quot;&gt;

&lt;imageview android:id=&quot;@+id/icon&quot;

android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;fill_parent&quot;

android:layout_alignParentTop=&quot;true&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:layout_marginRight=&quot;6dip&quot;/&gt;

&lt;textview android:id=&quot;@+id/row_toptext&quot;

android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;fill_parent&quot;

android:layout_toRightOf=&quot;@id/icon&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:layout_alignParentRight=&quot;true&quot;

android:singleLine=&quot;true&quot;
android:ellipsize=&quot;marquee&quot;/&gt;

&lt;/relativelayout&gt;
</pre>
<p>Ahora que ya hemos creado todos los <em>Layouts </em>necesarios ya podemos empezar a escribir código. Lo primero que vamos a hacer es crear una nueva clase llamada <strong>Local</strong>, cada línea del <em>ListView</em> contendrá un elemento de ésta clase.</p>
<p><strong><span style="font-size: medium;"><span style="color: #008000;">Local.java:</span></span></strong></p>
<pre class="brush: java; title: ; notranslate">

package com.mobiledev;

public class Local {

private String localName;
private int localImage;

public String getLocalName() {
return localName;
}
public void setLocalName(String localName) {
this.localName = localName;
}
public int getLocalImage() {
return localImage;
}
public void setLocalImage(int i) {
this.localImage = i;
}

}
</pre>
<p>Ya tenemos el contenido, lo siguiente que necesitamos es un <em>Adapter </em>que se encargue de pasar la información de cada elemento de la clase al ListView.</p>
<p><strong><span style="font-size: medium;"><span style="color: #008000;">IconListViewAdapter: (dentro de IconListView.java)</span></span></strong></p>
<pre class="brush: java; title: ; notranslate">

public class IconListViewAdapter extends ArrayAdapter&lt;local&gt; {

private ArrayList&lt;/local&gt;&lt;local&gt; items;

public IconListViewAdapter(Context context, int textViewResourceId, ArrayList&lt;/local&gt;&lt;local&gt; items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.iconrow, null);
}
Local o = items.get(position);
if (o != null) {

//poblamos la lista de elementos

TextView tt = (TextView) v.findViewById(R.id.row_toptext);
ImageView im = (ImageView) v.findViewById(R.id.icon);

if (im!= null) {
im.setImageResource(o.getLocalImage());
}
if (tt != null) {
tt.setText(o.getLocalName());
}
}
return v;
}
}
</pre>
<p>Como veis el <em>Adapter </em>se encarga de coger los datos que le vamos pasando y colocarlos en el <em>ListView</em>. Utilizando los métodos <em>findviewbyId </em>lo que conseguimos es acceder a los compoenentes de la fila y pasarle los datos que queramos. Por último sólo falta el código fuente de la aplicación, donde en un métido a parte nos encargaremos de indicar el texto y las imágenes que queremos añadir.</p>
<p>Éste proceso se debe hacer dentro del método onCreate de la aplicación principal, de ésta manera sólo lo haremos una vez y estará disponible durante todo el <a href="http://www.tech-xperience.com/mobiledev/android/ciclo-de-vida-de-una-activity-lifecycle/" target="_blank">LifeCycle</a>.</p>
<p><strong><span style="font-size: medium;"><span style="color: #008000;">IconListView.java:</span></span></strong></p>
<pre class="brush: java; title: ; notranslate">

package com.mobiledev;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class IconListView extends ListActivity {
private ArrayList&lt;/local&gt;&lt;local&gt; m_locals = null;
private IconListViewAdapter m_adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/*
* Al crear la clase se inicializa el ListView que muestra los men�s
*/

m_locals = new ArrayList&lt;/local&gt;&lt;local&gt;();
this.m_adapter = new IconListViewAdapter(this, R.layout.iconrow, m_locals);
setListAdapter(this.m_adapter);

inicializarLocales();

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Local local = (Local) l.getItemAtPosition(position);

Toast.makeText(this, local.getLocalName(),
Toast.LENGTH_LONG).show();
}

/*
* Inicializacion del mapa
*/

private void inicializarLocales(){

try {
m_locals = new ArrayList&lt;/local&gt;&lt;local&gt;();
Local o1 = new Local();
o1.setLocalName(&quot;Bancos&quot;);
o1.setLocalImage(R.drawable.visa);
Local o2 = new Local();
o2.setLocalName(&quot;Gasolineras&quot;);
o2.setLocalImage(R.drawable.gas);
Local o3 = new Local();
o3.setLocalName(&quot;Hospitales&quot;);
o3.setLocalImage(R.drawable.hospital);
Local o4 = new Local();
o4.setLocalName(&quot;Restaurantes&quot;);
o4.setLocalImage(R.drawable.restaurant);
Local o5 = new Local();
o5.setLocalName(&quot;Tiendas&quot;);
o5.setLocalImage(R.drawable.shop);
m_locals.add(o1);
m_locals.add(o2);
m_locals.add(o3);
m_locals.add(o4);
m_locals.add(o5);
Log.i(&quot;Locales añadidos &quot;, &quot;&quot;+ m_locals.size());
} catch (Exception e) {
Log.e(&quot;BACKGROUND_PROC&quot;, e.getMessage());
}

if(m_locals != null &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; m_locals.size() &gt; 0){
for(int i=0;i&lt;m_locals .size();i++)
m_adapter.add(m_locals.get(i));
}

m_adapter.notifyDataSetChanged();

}
</pre>
<p><strong><span style="color: #008000;"><span style="color: #000000;"><em> </em>[tweetmeme] </span></span>No es necesario que el <em>ListView </em>sea lo único que se vea dentro del <em>Layout</em>. Podéis añadir botones, otros layouts o lo que os apetezca. Éste código sólo se encarga de rellenar el <em>ListView </em>y no afectará a otros componentens. Como nota interesante hemos añadido el envio de mensajes al Log del sistema. Tal y como lo hemos echo veremos en Log el número de locales añadido al <em>ListView</em> aunque, como os imaginais, la utilidad del Log es muchísimo más amplia.</p>
<p style="text-align: center;"><img class="size-full wp-image-147 aligncenter" title="screenshot1" src="http://www.tech-xperience.com/mobiledev/wp-content/uploads/2009/08/screenshot1.png" alt="screenshot1" width="325" height="584" /></p>
<p><strong><span style="font-size: medium;"><span style="color: #008000;">Código fuente: </span></span></strong></p>
<p><table style="border: 1px solid #CCC;" cellpadding="3" width="100%">
  <tr>
    <td width="35">
      <img src="http://mobile.davidocs.com/wp-content/plugins/downloads-manager/img/icons/winrar.gif" alt="http://mobile.davidocs.com/wp-content/plugins/downloads-manager/img/icons/winrar.gif">
    </td>
    <td>
      <b>download:</b> <a href="http://mobile.davidocs.com/?file_id=3">IconListView</a> <small>(44.94KB)</small><br />
      <b>added:</b> 03/08/2009 <br />
      <b>clicks:</b> 2044 <br />
      <b>description:</b> ListView que se compone de un icono y un texto. <br />
    </td>
  </tr>
</table></m_locals></local></p>



Comparte:


	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmobile.davidocs.com%2Fandroid%2Fcomo-anadir-iconos-a-un-listview-en-android%2F&amp;t=C%C3%B3mo%20a%C3%B1adir%20iconos%20a%20un%20ListView%20en%20Android" title="Facebook"><img src="http://mobile.davidocs.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://bitacoras.com/anotaciones/http%3A%2F%2Fmobile.davidocs.com%2Fandroid%2Fcomo-anadir-iconos-a-un-listview-en-android%2F" title="Bitacoras.com"><img src="http://mobile.davidocs.com/wp-content/plugins/sociable/images/bitacoras.png" title="Bitacoras.com" alt="Bitacoras.com" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://meneame.net/submit.php?url=http%3A%2F%2Fmobile.davidocs.com%2Fandroid%2Fcomo-anadir-iconos-a-un-listview-en-android%2F" title="Meneame"><img src="http://mobile.davidocs.com/wp-content/plugins/sociable/images/meneame.png" title="Meneame" alt="Meneame" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.wikio.it/vote?url=http%3A%2F%2Fmobile.davidocs.com%2Fandroid%2Fcomo-anadir-iconos-a-un-listview-en-android%2F" title="Wikio IT"><img src="http://mobile.davidocs.com/wp-content/plugins/sociable/images/wikio.png" title="Wikio IT" alt="Wikio IT" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmobile.davidocs.com%2Fandroid%2Fcomo-anadir-iconos-a-un-listview-en-android%2F&amp;title=C%C3%B3mo%20a%C3%B1adir%20iconos%20a%20un%20ListView%20en%20Android&amp;bodytext=Hace%20poco%20a%C3%B1adimos%20un%20enlace%20a%20una%20gran%20cantidad%20de%20im%C3%A1genes%20e%20iconos%20para%20incluir%20en%20los%20Proyectos%20de%20Android.%20Pues%20bien%2C%20ya%20va%20siendo%20hora%20de%20darles%20utilidad.%20Vamos%20a%20a%C3%B1adir%20un%20icono%20a%20un%20ListView%2C%20de%20%C3%A9sta%20manera%20tendremos%20un%20dise%C3%B1o%20mucho%20m%C3%A1s" title="Digg"><img src="http://mobile.davidocs.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmobile.davidocs.com%2Fandroid%2Fcomo-anadir-iconos-a-un-listview-en-android%2F&amp;title=C%C3%B3mo%20a%C3%B1adir%20iconos%20a%20un%20ListView%20en%20Android&amp;notes=Hace%20poco%20a%C3%B1adimos%20un%20enlace%20a%20una%20gran%20cantidad%20de%20im%C3%A1genes%20e%20iconos%20para%20incluir%20en%20los%20Proyectos%20de%20Android.%20Pues%20bien%2C%20ya%20va%20siendo%20hora%20de%20darles%20utilidad.%20Vamos%20a%20a%C3%B1adir%20un%20icono%20a%20un%20ListView%2C%20de%20%C3%A9sta%20manera%20tendremos%20un%20dise%C3%B1o%20mucho%20m%C3%A1s" title="del.icio.us"><img src="http://mobile.davidocs.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.friendfeed.com/share?title=C%C3%B3mo%20a%C3%B1adir%20iconos%20a%20un%20ListView%20en%20Android&amp;link=http%3A%2F%2Fmobile.davidocs.com%2Fandroid%2Fcomo-anadir-iconos-a-un-listview-en-android%2F" title="FriendFeed"><img src="http://mobile.davidocs.com/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fmobile.davidocs.com%2Fandroid%2Fcomo-anadir-iconos-a-un-listview-en-android%2F&amp;t=C%C3%B3mo%20a%C3%B1adir%20iconos%20a%20un%20ListView%20en%20Android" title="HackerNews"><img src="http://mobile.davidocs.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fmobile.davidocs.com%2Fandroid%2Fcomo-anadir-iconos-a-un-listview-en-android%2F" title="Technorati"><img src="http://mobile.davidocs.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>


<br/><br/>

<p>Entradas relacionadas:<ol><li><a href='http://mobile.davidocs.com/android/como-instalar-el-teclado-del-htc-hero-en-el-htc-magic/' rel='bookmark' title='Permanent Link: Cómo instalar el teclado del HTC Hero en el HTC Magic'>Cómo instalar el teclado del HTC Hero en el HTC Magic</a></li>
<li><a href='http://mobile.davidocs.com/android/tratamiento-de-ficheros-xml-con-android/' rel='bookmark' title='Permanent Link: Tratamiento de ficheros XML con Android'>Tratamiento de ficheros XML con Android</a></li>
<li><a href='http://mobile.davidocs.com/android/coleccion-de-iconos-e-imagenes-para-android/' rel='bookmark' title='Permanent Link: Colección de iconos e imágenes para Android'>Colección de iconos e imágenes para Android</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://mobile.davidocs.com/android/como-anadir-iconos-a-un-listview-en-android/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

