simplecursoradapter not working

亡梦爱人 提交于 2019-12-12 09:11:43

问题


I am using a database, getting a cursor out of it and then using a simplecursoradapter to populate a listview. I can't seem to figure out why the app crashes on creating a new simplecursoradapter. My cursor is contained in a singleton object. I have a reference to it through the data variable in this class.

String[] columns = new String[] {"item", "quantity", "days"};
int[] to = new int[] { R.id.nametext, R.id.quantitytext, R.id.dayslefttext};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.itemrow, data.listCursor, columns, to);
listView1.setAdapter(sCA);

What does the simplecursoradapter do with the information passed to it in its constructor? Is one of the parameters in the wrong format? Here is the simple xml file for each row that I am using to just see if this works:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content">
  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Loading Screen" 
    android:id="@+id/nametext"/>
  <TextView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Loading Screen" 
    android:id="@+id/quantitytext"/>
  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Loading Screen" 
    android:id="@+id/dayslefttext"/>
</LinearLayout>

Here is how I load the cursors:

listCursor = myDataBase.query(ITEMS_TABLE, null, "location" +" = ?", new String[]{IN_SPECIAL_LIST}, null, null, "item");

I can't figure out what the problems is. I ran this through some debugging and the problem occurs when I I create the new simplecursoradapter.


回答1:


myDataBase.query(ITEMS_TABLE, null, ...

this null is your problem, it means that query will use SELECT * but there is no column _id in your table so you should use

new String[] {"YOURID AS _id", "item", "quantity", "days"}

SimpleCursorAdapter need _id column

YOURID could be ROWID (ROWID as _id) or any int or long ID from your DB



来源:https://stackoverflow.com/questions/5536795/simplecursoradapter-not-working

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