gpt4 book ai didi

android.widget.ListView 不是可以受此 SimpleCursorAdapter 限制的 View

转载 作者:行者123 更新时间:2023-11-29 22:16:11 24 4
gpt4 key购买 nike

我正在尝试用这种方法从 bbdd 获取一些数据

/**********************************************************************
* * Obten todos los nombres
*
*/

public Cursor getNombres(){


Cursor respuesta = db.rawQuery("select "+TABLE_ROW_ID+","+CNOMBRE+" from "+TABLE_NAME, null);

return respuesta;
}

在这个类(class)

  public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
//Creamos la instancia de DataBaseHelper, un cursor y aplicamos el metodo getNombres al cursor.
ayudabbdd = new DataBaseHelper(this);
Cursor nombresC;
nombresC = (Cursor) ayudabbdd.getNombres();
startManagingCursor(nombresC);

//Mientras el cursor no este vacio rellenamos la lista con el adaptador, que nos sirve para conectar unos datos con el listview.
if(nombresC!=null){
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.listatab, nombresC, new String[] { "nombre" }, new int[] { R.id.lista });
this.setListAdapter(adapter);
this.getListView().setTextFilterEnabled(true);
}



}

但是 log cat 返回给我

android.widget.ListView is not a  view that can be bounds by this SimpleCursorAdapter 

但在一些教程中,这是从 bbdd 获取数组的方法,他们没有问题

最佳答案

如果您阅读 SimpleCursorAdapter 的文档, 构造函数, 顺便说一句, 已被弃用, 从文档中获取 TextViews 的 id 作为第 5 个参数:

to->    The views that should display column in the "from" parameter. 
These should all be TextViews. The first N views in this list are
given the values of the first N columns in the from parameter.
Can be null if the cursor is not available yet.

相反,您提供的是 lista 的 id,它是一个 ListView。尝试使用此更改:

int[] to = new int[] { android.R.layout.simple_list_item_1 };
...
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listatab, nombresC, new String[] { "nombre" }, to);

另外,如果您的 Activity 扩展了 ListActivity,则您的 xml 中的 ListView 必须具有此 id:

android:id="@android:id/android:list"

看看这个example .

关于android.widget.ListView 不是可以受此 SimpleCursorAdapter 限制的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8424731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com