gpt4 book ai didi

android - 从 SQLite 填充 ListView

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

一旦我开始从我的数据库填充 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类

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;

}

最佳答案

CursorAdapter 确实需要 _id 列。

您可以做的是使用古老的“rowID 技巧”。
只需将 , rowID AS _id 添加到您的查询即可添加 rowID 并将其命名为 _id

所以,如果您的查询是这样的:

SELECT * FROM ...

改成

SELECT *, rowID AS _id FROM ...

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

关于android - 从 SQLite 填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33710577/

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