×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: joseifm
Added: Sep 23, 2014 3:50 PM
Modified: Sep 23, 2014 3:59 PM
Views: 1818
Tags: no tags
  1. // Main Class Activity
  2. import android.widget.AdapterView;
  3. import android.widget.ListView;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. ListView listView = (  ListView ) findViewById(R.id.listView);
  9. //Create Array
  10. final List items = new ArrayList();
  11. //we add to array the image and text -- Here we use the class Item.java
  12. items.add(new Item(R.drawable.folder_green, "text green");
  13. items.add(new Item(R.drawable.folder_red, "text red");
  14.  
  15. //Join the ListVIew with array Adapter,
  16. listView.setAdapter(new ItemAdapter(this, items));
  17.  
  18. // create a listener to know when an item has been pressed
  19.  
  20.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  21.             @Override
  22.             public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
  23.                         // Put your code here
  24.             }
  25.         }
  26.  
  27. //We create a class named Item.java, with Setter and Getter method
  28.  
  29. public class Item {
  30.     private int imagen;
  31.     private String texto;
  32.  
  33.      public Item(int imagen, String texto) {
  34.              super();
  35.              this.imagen = imagen;
  36.              this.texto = texto;
  37.      }
  38.  
  39.      public int getImagen() {
  40.              return imagen;
  41.      }
  42.  
  43.     public void setImagen(int imagen) {
  44.             this.imagen = imagen;
  45.     }
  46.  
  47.     public String getTexto() {
  48.              return texto;
  49.     }
  50.  
  51.     public void setTexto(String texto) {
  52.              this.texto = texto;
  53.     }
  54. }
  55.  
  56. //We create a class named ItemAdapter.java
  57.  
  58. import android.content.Context;
  59. import android.view.LayoutInflater;
  60. import android.view.View;
  61. import android.view.ViewGroup;
  62. import android.widget.BaseAdapter;
  63. import android.widget.ImageView;
  64. import android.widget.TextView;
  65.  
  66. public class ItemAdapter extends BaseAdapter {
  67.  
  68.     private Context context;
  69.     private List<Item> items;
  70.    
  71.  
  72.     public ItemAdapter(Context context, List<Item> items) {
  73.         this.context = context;
  74.         this.items = items;
  75.     }
  76.  
  77.     @Override
  78.     public int getCount() {
  79.         return this.items.size();
  80.     }
  81.  
  82.     @Override
  83.     public Object getItem(int position) {
  84.         return this.items.get(position);
  85.     }
  86.  
  87.     @Override
  88.     public long getItemId(int position) {
  89.         return position;
  90.     }
  91.  
  92.      @Override
  93.      public View getView(int position, View convertView, ViewGroup parent) {
  94.  
  95.           View rowView = convertView;
  96.  
  97.         if (convertView == null) {
  98.             // Create a new view into the list.
  99.             LayoutInflater inflater = (LayoutInflater) context
  100.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  101.             rowView = inflater.inflate(R.layout.activity_list_item, parent, false);
  102.         }
  103.  
  104.         ImageView imageItem = (ImageView) rowView.findViewById(R.id.imagenitem);
  105.         TextView txtTitle = (TextView) rowView.findViewById(R.id.textitem);
  106.  
  107.         imageItem.setText(tem.getTexto());
  108.         txtTitle.setImageResource(item.getImagen());
  109.  
  110.         return rowView;
  111.     }
  112.  
  113. }
  114.  
  115. //We create layout activity_list.xml
  116.  
  117. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  118.     xmlns:tools="http://schemas.android.com/tools"
  119.     android:layout_width="match_parent"
  120.     android:layout_height="match_parent"
  121.     android:paddingLeft="@dimen/activity_horizontal_margin"
  122.     android:paddingRight="@dimen/activity_horizontal_margin"
  123.     android:paddingTop="@dimen/activity_vertical_margin"
  124.     android:paddingBottom="@dimen/activity_vertical_margin"
  125.     tools:context="es.tecnoleganes.cloudtecnoleganes.list_item">
  126.  
  127.     <RelativeLayout
  128.         android:layout_width="fill_parent"
  129.         android:layout_height="fill_parent"
  130.         android:layout_alignParentTop="true"
  131.         android:layout_alignParentLeft="true"
  132.         android:layout_alignParentStart="true">
  133.  
  134.         <ImageView
  135.             android:layout_width="wrap_content"
  136.             android:layout_height="wrap_content"
  137.             android:id="@+id/imagenitem"
  138.             android:layout_alignParentTop="true"
  139.             android:layout_alignParentLeft="true"
  140.             android:layout_alignParentStart="true"
  141.             android:minHeight="20dp"
  142.             android:minWidth="20dp"
  143.             android:maxWidth="20dp"
  144.             android:maxHeight="20dp"
  145.              />
  146.  
  147.         <TextView
  148.             android:layout_width="wrap_content"
  149.             android:layout_height="wrap_content"
  150.             android:textAppearance="?android:attr/textAppearanceSmall"
  151.             android:text="txt"
  152.             android:id="@+id/textitem"
  153.             android:textColor="@android:color/white"
  154.             android:singleLine="true"
  155.             android:layout_alignParentTop="true"
  156.             android:layout_toRightOf="@id/imagenitem"
  157.             android:layout_alignTop="@+id/imagenitem" />
  158.   </RelativeLayout>
  159. </RelativeLayout>
  160.  
  161. //Layout Activity Main
  162. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  163.     xmlns:tools="http://schemas.android.com/tools"
  164.     android:layout_width="match_parent"
  165.     android:layout_height="match_parent"
  166.     android:paddingLeft="@dimen/activity_horizontal_margin"
  167.     android:paddingRight="@dimen/activity_horizontal_margin"
  168.     android:paddingTop="@dimen/activity_vertical_margin"
  169.     android:paddingBottom="@dimen/activity_vertical_margin"
  170.     tools:context=".MyActivity"
  171.     android:background="@android:color/black">
  172.  
  173.     <ListView
  174.         android:layout_width="wrap_content"
  175.         android:layout_height="wrap_content"
  176.         android:id="@+id/listView"
  177.         android:textColor="@android:color/white"
  178.         android:layout_alignParentLeft="true"
  179.         android:layout_alignParentStart="true" />
  180.  
  181. </RelativeLayout>
  182.