2

I'm new programming with android, and doing my first aplication i got something wrong with the lists. Here the code:

Cursor c = db.rawQuery("SELECT nombre FROM contactos", null);

        ArrayList<String> listaArray = new ArrayList<String>();
        ListView listadoContactos = (ListView)findViewById(R.id.listViewListaContactos);

        if (c.moveToFirst())
        {
            do {
                listaArray.add(c.getString(0));
            } while(c.moveToNext());
        }

        //Creamos un adaptador y lo asignamos al ListView.
        ArrayAdapter<String> adaptadorLista = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listaArray);

        listadoContactos.setAdapter(adaptadorLista);

When i see the result, my list its showing only the first item in the db that i'm querying. Can you help me, please?

Thanks in advice! George.

PS: "nombre" is the column 0. That is why i'm writting "getString(0)", because i just want to show 1 column of each row.

JMasia
  • 103
  • 1
  • 1
  • 6

3 Answers3

0

You could use a SimpleCursorAdapter and pass it the cursor directly:

Adapter adapter = new SimpleCursorAdapter(
    this,
    c,
    android.R.layout.simple_list_item_1,
    new String[] { "nombre" },
    new int[] { android.R.id.text1 },
    0);
listadoContactos.setAdapter(adapter);

and then you don't have to step through and build the list yourself.

Matthew
  • 44,826
  • 10
  • 98
  • 87
0

Try to use SimpleCursorAdapter

// NOTE your query result should have id-field, named "_id"
    Cursor c = db.rawQuery("SELECT nombre, nombre_id as _id FROM contactos", null);

    SimpleCursorAdapter adaptorLista = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, new String[]{"nombre"}, new int[]{android.R.id.text1} );
            listadoContactos.setAdapter(adaptadorLista);

Here android.R.layout.simple_list_item_1 is a layout of each row in ListView, new String[]{"nombre"} names of fields in cursor which values are set to TextViews in ListView row. TextView ids specified in the last argument new int[]{android.R.id.text1}

Max Komarychev
  • 2,836
  • 1
  • 21
  • 32
  • Thank you very much. It worked for me. Now, i'm getting troubles showing the listview, it's shown half-cut. Any suggestion? – JMasia Mar 16 '11 at 21:36
  • Show, please, screen layout and what do you mean saying "half-cut" ? – Max Komarychev Mar 17 '11 at 11:28
  • Sorry for my bad english, i mean this with "half-cut": http://goo.gl/BYSGo Thank you again. – JMasia Mar 17 '11 at 18:14
  • I need to look at xml with screen layout – Max Komarychev Mar 17 '11 at 18:22
  • Here you have – JMasia Mar 17 '11 at 18:40
  • I think it is beacause you have ListView inside a ScrollView. There are some problems can appear if combining two or more scrolling views. Read this question http://stackoverflow.com/questions/5312592/how-can-i-get-my-listview-to-scroll and answers, maybe it helps you – Max Komarychev Mar 17 '11 at 18:52
  • Provided layout is the same as that used for screenshot? The button is present on the picture but is missing in xml (or does button is inside the row of the list?). Post actual layout and screenshot, please. (update first post and use formatting tools for xml - it's hard to read, thanks) – Max Komarychev Mar 18 '11 at 19:50
0

Use a CursorAdapter instead of an ArrayListAdapter. You are making extra work for yourself by copying the cursor into an Array just to feed it to the ListView. Here is a tutorial on CursorAdapters

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156