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

橙三吉。 提交于 2019-12-11 10:58:04

问题


Im trying to get some data from bbdd with this method

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

public Cursor getNombres(){


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

       return respuesta;
}

in this 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);  
     }



     }

But log cat return me that

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

But in some tutorials this is the way to get an array from a bbdd and they don't have problem


回答1:


If you read the doc for SimpleCursorAdapter, the constructor, which by the way is deprecated, gets as 5th parameter the ids of TextViews, from the doc:

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.

Instead you are giving the id of lista, which is a ListView. Try to change using this:

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

En plus, if your activity extends ListActivity, the ListView in your xml must have this id:

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

Take a look to this example.



来源:https://stackoverflow.com/questions/8424731/android-widget-listview-is-not-a-view-that-can-be-bounds-by-this-simplecursorada

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!