如何只查看数据库表中具有特定 ID 的项目?
在我的数据库中,我有 2 个表。对于表,我只想显示第二列中具有特定 id 的项目。我有一个适用于表中所有项目的适配器,但我不知道它应该是什么样子才能仅查看我需要的项目。欢迎任何想法。
这是我的适配器:
private void populate(){
c=prod.fetchAll();
startManagingCursor(c);
adapter=new SimpleCursorAdapter(this,R.layout.listproduct,c,
new String[] {ProductDbAdapter.KEY_ITEM,ProductDbAdapter.KEY_QUANTITY,ProductDbAdapter.KEY_UNITS},
new int[] {R.id.prod1,R.id.prod2,R.id.prod3});
lv.setAdapter(adapter);
}
ProductDbAdapter 的代码是:
public class ProductDbAdapter extends AbstractDbAdapter {
public static final String KEY_LISTID = "list_id";
public static final String KEY_ITEM = "item";
public static final String KEY_QUANTITY="quantity";
public static final String KEY_UNITS="units";
public static final String KEY_ROWID = "_id";
private static final String DATABASE_TABLE = "products";
public ProductDbAdapter(Context ctx) {
super(ctx);
}
public long create(Integer listid, String item,String quantity,String units) {
ContentValues args = new ContentValues();
args.put(KEY_LISTID,listid);
args.put(KEY_ITEM,item);
args.put(KEY_QUANTITY,quantity);
args.put(KEY_UNITS,units);
return mDb.insert(DATABASE_TABLE, null,args);
}
public boolean updateproduct(long rowId,Integer listid, String item,String quantity,String units) {
ContentValues values = new ContentValues();
values.put(KEY_LISTID, listid);
values.put(KEY_ITEM, item);
values.put(KEY_QUANTITY, quantity);
values.put(KEY_UNITS,units);
return mDb.update(DATABASE_TABLE, values, KEY_ROWID + "="+ rowId, null) > 0;
}
public boolean delete(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor fetchAll() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LISTID,KEY_ITEM,KEY_QUANTITY,
KEY_UNITS}, null, null, null, null, null);
}
public Cursor fetch(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_LISTID,KEY_ITEM,KEY_QUANTITY, KEY_UNITS}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
我想做的是仅显示具有特定 list_id 的项目。
In my database I have 2 tables. For a table I want to display only the items which has a certain id in the second column. I have an adapter for all the items from the table but I don't know how should it look like for viewing only the items I need. Any idea is welcome.
Here is my adapter :
private void populate(){
c=prod.fetchAll();
startManagingCursor(c);
adapter=new SimpleCursorAdapter(this,R.layout.listproduct,c,
new String[] {ProductDbAdapter.KEY_ITEM,ProductDbAdapter.KEY_QUANTITY,ProductDbAdapter.KEY_UNITS},
new int[] {R.id.prod1,R.id.prod2,R.id.prod3});
lv.setAdapter(adapter);
}
The code for ProductDbAdapter is :
public class ProductDbAdapter extends AbstractDbAdapter {
public static final String KEY_LISTID = "list_id";
public static final String KEY_ITEM = "item";
public static final String KEY_QUANTITY="quantity";
public static final String KEY_UNITS="units";
public static final String KEY_ROWID = "_id";
private static final String DATABASE_TABLE = "products";
public ProductDbAdapter(Context ctx) {
super(ctx);
}
public long create(Integer listid, String item,String quantity,String units) {
ContentValues args = new ContentValues();
args.put(KEY_LISTID,listid);
args.put(KEY_ITEM,item);
args.put(KEY_QUANTITY,quantity);
args.put(KEY_UNITS,units);
return mDb.insert(DATABASE_TABLE, null,args);
}
public boolean updateproduct(long rowId,Integer listid, String item,String quantity,String units) {
ContentValues values = new ContentValues();
values.put(KEY_LISTID, listid);
values.put(KEY_ITEM, item);
values.put(KEY_QUANTITY, quantity);
values.put(KEY_UNITS,units);
return mDb.update(DATABASE_TABLE, values, KEY_ROWID + "="+ rowId, null) > 0;
}
public boolean delete(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor fetchAll() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LISTID,KEY_ITEM,KEY_QUANTITY,
KEY_UNITS}, null, null, null, null, null);
}
public Cursor fetch(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_LISTID,KEY_ITEM,KEY_QUANTITY, KEY_UNITS}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
What I am trying to do is to display only the items with a certain list_id..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您只想要具有特定 id 的项目时,为什么要请求返回所有项目的游标?更改数据库查询,以便仅获取具有您在数据库查询的 where 语句中定义的 id 的项目。
让数据库为您过滤正确的项目,这不是适配器的工作。
Why do you request a cursor which returns all item when you only want items with a certain id? Change your database query so you only get the items with the ids you define in the where-statement of your database query.
Let the database so the job of filtering the correct items for you, it's not the job of an adapter.
在 ProductDbAdapter 中,尝试类似...
In
ProductDbAdapter
, Try something like...