Android ArrayAdapter/ListView不工作

Android ArrayAdapter/ListView不工作,android,listview,cursor,android-arrayadapter,Android,Listview,Cursor,Android Arrayadapter,我是一名新的android编程人员,在我的第一次应用中,我发现列表有问题。 代码如下: Cursor c = db.rawQuery("SELECT nombre FROM contactos", null); ArrayList<String> listaArray = new ArrayList<String>(); ListView listadoContactos = (ListView)findViewById(R.id.lis

我是一名新的android编程人员,在我的第一次应用中,我发现列表有问题。 代码如下:

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);
Cursor c=db.rawQuery(“从contactos中选择nombre”,null);
ArrayList listaArray=新的ArrayList();
ListView listadoContactos=(ListView)findViewById(R.id.listViewListaContactos);
if(c.moveToFirst())
{
做{
add(c.getString(0));
}而(c.moveToNext());
}
//Cliamos un Adapter y lo asignamos al ListView。
ArrayAdapter AdapterLista=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,listaArray);
setAdapter(adaptedorlista);
当我看到结果时,我的列表只显示我正在查询的数据库中的第一项。你能帮帮我吗

谢谢你的建议!乔治


PS:“nombre”是第0列。这就是为什么我要写“getString(0)”,因为我只想显示每行的1列。

您可以使用SimpleCursorAdapter并直接将光标传递给它:

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);

然后,您就不必自己单步执行并构建列表。

您可以使用SimpleCorsorAdapter并直接将光标传递给它:

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);

然后你就不必自己一步一步地构建列表了。

尝试使用SimpleCorsorAdapter

// 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);
这里
android.R.layout.simple\u list\u item\u 1
是ListView中每一行的布局,
newstring[]{“nombre”}
cursor中的字段名称,这些字段的值设置为ListView行中的TextView。在最后一个参数
new int[]{android.R.id.text1}
中指定的TextView id尝试使用SimpleCorsorAdapter

// 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);
这里
android.R.layout.simple\u list\u item\u 1
是ListView中每一行的布局,
newstring[]{“nombre”}
cursor中的字段名称,这些字段的值设置为ListView行中的TextView。在最后一个参数
new int[]{android.R.id.text1}
中指定的TextView id使用一个而不是ArrayListAdapter。通过将光标复制到一个数组中,只是为了将其馈送到ListView,您正在为自己做额外的工作。

使用a而不是ArrayListAdapter。通过将光标复制到一个数组中,只是为了将其馈送到ListView,您正在为自己做额外的工作。

看到非英语标识符对我来说总是很奇怪。看到非英语标识符对我来说总是很奇怪。非常感谢。这对我有用。现在,我在显示listview时遇到了麻烦,它显示为半切。有什么建议吗?请展示屏幕布局,你说“半截”是什么意思?很抱歉我的英语不好,我的意思是“半截”:再次感谢。我需要看一下带有屏幕布局的xml,谢谢。这对我有用。现在,我在显示listview时遇到了麻烦,它显示为半切。有什么建议吗?请展示屏幕布局,你说“半截”是什么意思?很抱歉我的英语不好,我的意思是“半截”:再次感谢。我需要看一下带有屏幕布局的xml