SHOW:
|
|
- or go back to the newest paste.
| 1 | package androidya.proyecto006; | |
| 2 | ||
| 3 | import android.app.Activity; | |
| 4 | import android.os.Bundle; | |
| 5 | import android.view.View; | |
| 6 | import android.widget.AdapterView; | |
| 7 | import android.widget.AdapterView.OnItemClickListener; | |
| 8 | import android.widget.ArrayAdapter; | |
| 9 | import android.widget.ListView; | |
| 10 | import android.widget.TextView; | |
| 11 | ||
| 12 | public class ListviewActivity extends Activity {
| |
| 13 | /** vectores donde almacenamos los paises y sus habitantes*/ | |
| 14 | private String[] paises={"Argentina","Chile","Paraguay","Bolivia","Peru",
| |
| 15 | "Ecuador","Brasil","Colombia","Venezuela","Uruguay"}; | |
| 16 | private String[] habitantes={"40000000","17000000","6500000","10000000","30000000",
| |
| 17 | "14000000","183000000","44000000","29000000","3500000"}; | |
| 18 | ||
| 19 | /** objetos de tipo TextView y ListView*/ | |
| 20 | private TextView tv1; | |
| 21 | private ListView lv1; | |
| 22 | /** Called when the activity is first created. */ | |
| 23 | @Override | |
| 24 | public void onCreate(Bundle savedInstanceState) {
| |
| 25 | /** obtenemos la referencia a los dos objetos*/ | |
| 26 | super.onCreate(savedInstanceState); | |
| 27 | setContentView(R.layout.main); | |
| 28 | tv1=(TextView)findViewById(R.id.tv1); | |
| 29 | lv1 =(ListView)findViewById(R.id.listView1); | |
| 30 | /** Creamos un objeto de la clase ArrayAdapter */ | |
| 31 | ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, paises); | |
| 32 | lv1.setAdapter(adapter); | |
| 33 | ||
| 34 | /** Llamamos al método setOnItemClicListener de la clase ListView y le pasamos como parámetro una clase anónima que implementa la interfaz OnItemClickListener*/ | |
| 35 | lv1.setOnItemClickListener(new OnItemClickListener() {
| |
| 36 | @Override | |
| 37 | - | /** Dentro del método onItemClick modificamos el contenido del TextView con el nombre del país y la |
| 37 | + | /** Dentro del método onItemClick modificamos el contenido del TextView con el nombre del país y la cantidad de habitantes de dicho país. Este método recibe en el tercer parámetro la posición del |
| 38 | - | cantidad de habitantes de dicho país. Este método recibe en el tercer parámetro la posición del |
| 38 | + | |
| 39 | public void onItemClick(AdapterView<?> parent, View v, int posicion, long id) {
| |
| 40 | tv1.setText("Población de "+ lv1.getItemAtPosition(posicion) + " es "+ habitantes[posicion]);
| |
| 41 | } | |
| 42 | }); | |
| 43 | } | |
| 44 | ||
| 45 | } |