2011-09-06 118 views
0

在我的應用我使用一個ListView與他們的菜名來顯示的美食菜單的ListView與SimpleCursorAdapter

這是我使用適配器從查詢數據綁定到列表視圖代碼:

private void getDishes() { 

    DBAdapter db = new DBAdapter(this); 

    db.open(); 
    Cursor cursor = db.getAllDishes(); 
    cursor.moveToFirst(); 
    Log.w("ppp","kk - " + cursor.moveToFirst()); 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      R.layout.list_item, cursor, new String[] { DBAdapter.DishName }, 
      new int[] {R.id.dishName }); 

    setListAdapter(adapter); 
    db.close(); 
} 

我的查詢代碼如下所示:

public Cursor getAllDishes() 
{ 
    return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null); 
} 

真的很感謝所有幫助/意見,因爲我一直停留在這將近一週了。

+0

我沒看過的代碼,你可能需要改善你的問題Stion的。什麼問題和什麼導致你卡住? –

+0

對不起,我忘了添加,應用程序似乎無法運行在第一篇文章中的代碼,我做了一個logcat來檢查查詢是否工作,並且它返回true,這意味着值爲null是正確的? – user930008

+0

通過Listview檢查本教程:http://www.vogella.de/articles/AndroidListView/article.html –

回答

0

你能明確解釋你的問題嗎?

我建議改變

public Cursor getAllDishes() 
{ 
    return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null); 
} 

public Cursor getAllDishes() 
    { 
    Cursor dishes = db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null); 
if(dishes != null) 
dishes.moveToFirst(); 
     return dishes; 
    } 

然後

getDishes()方法寫這樣

private void getDishes() { 

    DBAdapter db = new DBAdapter(this); 

    db.open(); 
    Cursor cursor = db.getAllDishes(); 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(YourActivity.this, 
      R.layout.list_item, cursor, new String[] { DBAdapter.DishName }, 
      new int[] {R.id.dishName }); 
    setListAdapter(adapter); 
    db.close(); 
} 
相關問題