从SQLite填写ListView

时间:2015-11-14 16:25:06

标签: android sqlite listview

一旦我开始从我的数据库中填充ListView,我就会面临崩溃 使用简单的CursorAdapter

并且logcat显示此消息

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.osamawaosamawa.medcine/com.example.osamawaosamawa.medcine.ppllistViews}: java.lang.IllegalArgumentException: column '_id' does not exist

_id列是什么?我在那个班上没有这个

listview java class

public class ppllistViews extends Activity {
DB db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ppllistview);
    db=new DB(this);
    pupulatelistview();

  }

   public void pupulatelistview(){

    Cursor cursor = db.getpplNameData();
    startManagingCursor(cursor);

    String [] pplinformation = new String[]{
            DB.col1T2
    };
    int [] maping = new int[]{
            R.id.PPLID
    };

    SimpleCursorAdapter mycursorAdapter =
            new    SimpleCursorAdapter(this,R.layout.pplnamelistxml,cursor,pplinformation,maping);


    ListView ppldatalistview = (ListView)findViewById(R.id.listViewpplname);
    ppldatalistview.setAdapter(mycursorAdapter);
}

我已经在我的DBCLASS中添加了这一行

public final static  String colrowid = "_id";

我的查询,我可以在这里添加

  public Cursor getpplNameData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor result = db.rawQuery("select * from " + Table_name2, null);
    return result;

}

1 个答案:

答案 0 :(得分:2)

CursorAdapters确实需要_id列。

你可以做的是使用旧的" rowID技巧" 只需在查询中添加, rowID AS _id即可添加rowID并将其命名为_id

因此,如果您的查询类似于:

SELECT * FROM ...

将其更改为

SELECT *, rowID AS _id FROM ...

rowID是一个隐藏字段,几乎可以替代_id字段。