Aplicación de Android: cómo mostrar una lista de elementos y hacer que se pueda hacer clic en ellos

I need to display a list of text items to the screen and make them clickable. So it would be something like a list of links on a web application.

How can I do that in an Android Activity screen?

It would be some random number of items that I have to pull from a db and display all as links.

¿Alguna idea de cómo se puede hacer eso?

preguntado el 09 de marzo de 12 a las 16:03

4 Respuestas

You should read the doc about ListaActividad, ListView y seguir el Tutorial de Hello ListView.

respondido 09 mar '12, 16:03

Yes you can do it. Create a DataExchange class to fetch it from Db.. Store the Strings in an Array.

Create a ArrayAdapter to display the array of Strings you got from the database.

por ejemplo

public class AndroidListViewActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {

    // storing string resources into Array
    String[] numbers = {"one","two","three","four"}
     // here you store the array of string you got from the database

    // Binding Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label,       numbers));
    // refer the ArrayAdapter Document in developer.android.com
    ListView lv = getListView();

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item 
          String num = ((TextView) view).getText().toString();

          // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          // sending data to new activity
          i.putExtra("number", num);


The secondActivity to display the Particular item you have clicked should be

public class SingleListItem extends Activity{
public void onCreate(Bundle savedInstanceState) {

    TextView txtProduct = (TextView) findViewById(R.id.product_label);

    Intent i = getIntent();
    // getting attached intent data
    String product = i.getStringExtra("number");
    // displaying selected product name


you have to create various layout files accordingly.. Hope this helps you :)

Respondido 26 Jul 15, 20:07

Deberías usar un ListView. It's very simple, just create a ListActivity, put your items inside an Adapter and then set it as the Adapter de tu ListActivity.

You can read more about ListViews aquí

respondido 09 mar '12, 18:03

There is also a new paradigm called ListFragment.

I have used ListViews before but prefer the fragments approach now - it's just very straight forward and quite flexible esp on tablets since the interation with another area on the screen when selecting an item is quite flexible and only requires very little code.

Solo un ejemplo:

public class Select_FoodCategories_Fragment extends android.app.ListFragment {
    private static final boolean DEBUG = true;

    public void onCreate(Bundle savedInstanceState) {
    if (DEBUG)
        Log.i(this.getClass().getSimpleName(), " ->"
            + Thread.currentThread().getStackTrace()[2].getMethodName());


    public void onActivityCreated(Bundle savedInstanceState) {
    if (DEBUG)
        Log.i(this.getClass().getSimpleName(), " ->"
            + Thread.currentThread().getStackTrace()[2].getMethodName());
    HoldingActivity a = (HoldingActivity) getActivity();
    //accessing a variable of the activity is easy
    a.visibleListViewInFragment = getListView();

    List<XYZ> listTodisplay = a.getListToDisplay();

    MyAdapter adapter = new MyAdapter(
        getActivity(), 0, listTodisplay);


    public void onListItemClick(ListView l, View v, int position, long id) {
    if (DEBUG)
        Log.i(this.getClass().getSimpleName(), " ->"
            + Thread.currentThread().getStackTrace()[2].getMethodName());
        XYZ item = (XYZ) getListAdapter()



Más información aquí: http://developer.android.com/reference/android/app/ListFragment.html

By the way, I find it really worth it to get familiar with the new fragments concept - it just makes live much easier - esp on tablets!

ps I left the debug statements in on purpose - since it helps alto to understand the whole concept much faster in my experience

respondido 09 mar '12, 23:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.