我在视图中有一个微调器和一个列表视图。数据来自sqlite。 spinner数据来自类别表(Cid,Cname)Cname是在spinner中显示的,listview数据来自Items表(Iid,Iname,Cid)Iname是在listview中显示的。 cid是项目表中的外键。当微调器选择更改时,我想更改列表视图数据。这是我到目前为止所做的... 下面是我的微调器,其中Cname是从sqlite填充的
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) { dbRepo = new DBRepo(getApplicationContext());
String pos = (String) parent.getSelectedItemPosition();
String cid = dbRepo.getId(pos);
Toast.makeText(getApplicationContext(), cid, Toast.LENGTH_LONG).show();
final ArrayList<Support> list = dbRepo.getItems(cid);
adapter = new Custom(Category.this, R.layout.view_entry, list);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
这是获取微调器中所选项目的cid的代码
public String getId(String selected) {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT Cid from Catogeries where Cname=\"" + selected+ "\"";
Cursor c = db.rawQuery(query, null);
String s = null;
if (c.moveToFirst()) {
while (c.moveToNext()) {
s = c.getInt(c.getColumnIndex("Cid"));
}
}
c.close();
db.close();
Log.e("CID", " " + s);
return s;
}
这是从db
获取listview中数据的方法 public ArrayList<Support> getItems(String cid){
ArrayList<Support> list = new ArrayList<Support>();
SQLiteDatabase db = this.getReadableDatabase();
// Cursor c = db.query(TABLE_ITEM, new String[]{KEY_IID, KEY_INAME, KEY_CID}, KEY_CID + "=?", new String[]{cid}, null, null, null,null);
String select = "SELECT * from items_table where Cid = "+cid;
Cursor c = db.rawQuery(select, null);
String res;
int j = c.getColumnIndex(Support.KEY_INAME);
if(c.moveToFirst()){
do {
Support item = new Support();
item.setItemID(c.getString(0));
item.setItemName(c.getString(1));
item.setCategoryID(c.getString(2));
list.add(item);
}while (c.moveToNext());
}
c.close();
db.close();
return list;
}
现在cid正在吐司中显示但是在将cid传递给getitems()以获取listview.cid中没有显示任何内容的项目在日志中的getItems()中显示
public ArrayList<Support> getItems(String cid) {
ArrayList<Support> itemlist = new ArrayList<Support>();
SQLiteDatabase db = this.getReadableDatabase();
Log.e("CID is", cid);
String selectQuery = "SELECT * FROM " + TABLE_ITEM;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
while (c.moveToNext()) {
Support item = new Support();
item.setItemID(c.getString(0));
item.setItemName(c.getString(1));
item.setCategoryID(c.getString(2));
itemlist.add(item);
}
}
c.close();
db.close();
return itemlist;
}
上面的代码显示没有传递cid的项目,但是当我通过cid时listview显示为空并且他们在logcat中没有显示错误
终于解决了我的问题,因为cid使用trim函数后的空格,同时将从webservice获取的数据存储到sqlite已经为我做了
答案 0 :(得分:0)
public ArrayList<Support> getItems(String cid){
ArrayList<Support> list = new ArrayList<Support>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = database.rawQuery(SELECT * FROM items_table WHERE Cid =? ", new String[] {cid}, null);
Support item = new Support( );
if(cursor!=null)
{
while(cursor.moveToNext())
{
String ID= cursor.getString(cursor.getColumnIndexOrThrow(YOUR_DatabaseHelper.id_column));
// same with the others
item.add(ID);
list.add(item);
}
}
cursor.close();
db.close();
return list;
}