2011-12-11 45 views
0

我是新來的ExpandableListView,我非常需要你的幫助。Android ExpandableListView與SimpleCursorTreeAdapter不刷新

我正在使用下面的代碼創建一個ExpandableListView與SimpleCursorTreeAdapter,因爲我從SQLite數據庫創建我的數據。 它一切正常,但我找不到解決方案如何添加一些項目到父ListView(現在)。 我想通過點擊TextView來添加一個類別。

public class ListViewExp extends ExpandableListActivity { 
    private DBAdapter db; 
    private Cursor getMatchigItems; 
    private Cursor getAllCat; 
    private ExpandableListView expListView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.testexpview); 
     TextView tv1 = (TextView) findViewById(R.id.testtext); 
     db = new DBAdapter(this); 
     db.open(); 
     getAllCat = db.getAllCategories(1); 
     startManagingCursor(getAllCat); 

     final SimpleCursorTreeAdapter cursorTreeAdapter = new SimpleCursorTreeAdapter(
       getApplicationContext(), 
       getAllCat, 
       R.layout.group_row, 
       R.layout.group_row, 
       new String[]{db.KEYCAT_TXTCATEGORYNAME, db.KEYCAT_ROWID}, 
       new int[]{R.id.groupname}, 
       R.layout.child_row, 
       R.layout.child_row, 
       new String[]{db.KEYITEMS_TXTITEMNAME, db.KEYITEMS_ROWID, db.KEYITEMS_ROWID}, 
       new int[]{R.id.childname,R.id.rgb}) { 

      @Override 
      protected Cursor getChildrenCursor(Cursor groupCursor) { 
       db.open(); 
       getMatchigItems = db.getMatchingItems(groupCursor.getLong(0), 1); 
       startManagingCursor(getMatchigItems); 
       return getMatchigItems; 
      } 
     }; 
     expListView = getExpandableListView(); 
     setListAdapter(cursorTreeAdapter); 

     tv1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       db.open(); 
       db.addMyCategory("TestCat1", ""); 
//    getAllCat.requery(); 
       cursorTreeAdapter.notifyDataSetChanged(); 
      } 
     }); 
     db.close(); 
    } 
} 

添加到數據庫工作正常,但只有重新啓動我的應用程序時才能看到新項目。 我試圖重新查詢遊標getAllCat.requery(); ,但是當我這樣做時,它會刷新列表,但它是空的(即使顯示更舊的項目也是如此),就像沒有數據一樣。

任何人都可以請給我一些關於如何解決這個問題的指導? 我錯過了什麼嗎?

我應該做onResume覆蓋還是應該完全不同? 非常感謝! 此致敬禮!

+0

對不起奇怪找到答案後立即...'getAllCat = db.getAllCategories(1);'\t \t \t'cursorTreeAdapter.changeCursor(getAllCat);'什麼更好的建議,歡迎。謝謝 – PacQ

+0

我在做類似的東西這裏http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader – toobsco42

回答

1

對不起。奇怪的是,我已經在這裏提出要求之後找到了答案。不是我的意圖。但是,有人會使用它。

我已經重新初始化我的光標,並改變了它的適配器

getAllCat = db.getAllCategories(1); 
cursorTreeAdapter.changeCursor(getAllCat); 

這是什麼的onClick塊貌似現在:

public void onClick(View v) { 

       db.open(); 
       db.addMyCategory("TestCat7", ""); 
       getAllCat = db.getAllCategories(1); 
       cursorTreeAdapter.changeCursor(getAllCat); 
       cursorTreeAdapter.notifyDataSetChanged(); 
       Toast.makeText(getApplicationContext(), "KLIK", Toast.LENGTH_SHORT).show(); 
      } 

什麼更好的建議,仍然歡迎。 感謝