0

嗨im显示一个ListView与SimpleCursorAdapter和alternat ListItem样式,我想要动态更新列表,如果用户在视图和更新发生。在我的ListActivity我在我的适配器从SimpleCursorAdapter延伸做动态更新ListView与扩展SimpleCursorAdapter和替代LiteItem样式

adapter.swapCursor(newCursor); 
//notify 

我有两个重载方法

@Override 
     public int getViewTypeCount() {   
      return 2; 
     } 

@Override 
    public int getItemViewType(int position) { 
      adapCursor.moveToPosition(position);//adapCursor is being set in the constructor 
     int i= adapCursor.getInt(adapterCursor.getColumnIndexOrThrow("isactive")); 

     return i==1?i:0; 

     } 

现在的问题是,当我更新适配器更新的游标在getItemViewType方法中不可用,因此会抛出索引超出范围的异常...

我怎样才能获得更新的光标,还是回到正确ItemViewType

问候

回答

0

可以使用getCursor()方法以获得适配器所基于的Cursor的正确引用,即使在更改之后swapCursor()。这样您就不需要手动保持/更新对Cursor的引用。

1

找到了解决办法

只是overrided的swapCursor方法

 @Override 
     public Cursor swapCursor(Cursor c){   
      super.swapCursor(c); 
      adapCursor=c; 
      return c;    
     } 
+0

或者你可以使用'getCursor()'而不是你的参考。 – Luksprog

+0

@Luksprog更清洁的方式,我不必调用'super.swapCursor'把它放在答案中,我会接受它... tnx的信息 – dakait