更改CursorAdapter的游标会产生staleDataException [英] changing cursor of CursorAdapter gives staleDataException

查看:83
本文介绍了更改CursorAdapter的游标会产生staleDataException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以经过大量的网站和博客后,我终于决定问这个'不是那么常见问题'。



我有2个listViews。一个是LV类,另一个是LV项。功能非常简单 - 当用户点击类别LV中的类别单元格时,与该类别匹配的所有项目都应过滤并显示在项目LV中。我这样做是通过为数据库中的每个项目分配'category integer'。因此,当点击一个类别时,会获取其整数id,并且在光标中加载项目数据库中与其匹配的所有ID。



问题:我使用adapter.changeCursor(newCursor)方法更改适配器中的基础数据。根据文档,此方法替换并关闭先前加载的游标。我收到此错误



11-08 11:54:15.939:E / AndroidRuntime(2056):android.database.StaleDataException:试图访问已关闭的CursorWindow。最可能的原因:在调用此方法之前光标已停用。



下面是代码解释:



 itemAdapter = new CategoryCursorAdapter( this ,itemCursor,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER,  item_name 1 ,date,db); 
categoryLV.setOnItemClickListener( new AdapterView.OnItemClickListener(){

@ Override
public void onItemClick(AdapterView<?> arg0,View arg1,< span class =code-keyword> int arg2,
long arg3){
// TODO自动生成的方法存根
字符串 s =( (TextView)arg1.findViewById(R.id.category_name))。getText()。toString();

if (s.equalsIgnoreCase ( all)){

itemAdapter.changeCursor(null);
itemAdapter.notifyDataSetChanged();
}


else {
itemAdapter.changeCursor(db.getItemCursor(s));
itemAdapter.notifyDataSetChanged();

}



}

});





我在CursorAdapter类中收到错误:



  public   void  bindView(查看视图,上下文上下文,光标游标){
/ / TODO自动生成的方法存根
如果(mcolumn == CATEGORY_LV) {
TextView tv =(TextView)view.findViewById(R.id.category_name);
tv.setText(mCursor.getString(mCursor.getColumnIndex(mColumnName)));
tv.setTextSize( 20 );
}

if (mcolumn == ITEM_LV){
TextView item =(TextView)view.findViewById(R .id.item_name);
TextView stock =(TextView)view.findViewById(R.id.stock_status);

// 我在这个地方得到了staleDataException
< span class =code-sdkkeyword> String s = mCursor.getString(mCursor.getColumnIndex(mColumnName));
按钮拖动=(按钮)view.findViewById(R.id.drag_button);
LinearLayout lv =(LinearLayout)view.findViewById(R.id.layout);
drag.setFocusable(false);
item.setText(s);
item.setTextSize( 20 );
stock.setText( 股票: + mdb.getStockStatus(mdb.getItemCode(s) ),mDate));
stock.setTextSize( 15 );
}
}





这里需要注意的一点是我使用相同的Adapter类来为两者提供电源listViews。但是我不认为这可能是个问题。

解决方案

什么是mCursor?你是怎么创造它的?为什么不使用参数

 cursor 

来自

  public  < span class =code-keyword> void  bindView(视图视图,上下文上下文,光标游标)


okay, so after going through tons of sites and blogs I have finally decided to ask this 'not so FAQ'.

I have 2 listViews. One as the 'category LV' and other as the 'item LV'. The functionality is quite simple- When the user clicks a category cell in the 'category LV', all the items matching that category should filter and appear in the 'item LV'. I do this by assigning the 'category integer' to every item in the database. So, when a category is tapped its integer id is taken and all the ids matching with it in the item database are loaded in the cursor.

PROBLEM: I use the adapter.changeCursor(newCursor) method to change the underlying data in the adapter. According to the docs, this method replaces and closes the previously loaded cursor. I am getting this error

11-08 11:54:15.939: E/AndroidRuntime(2056): android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.

Down below is the code explained:

itemAdapter=new CategoryCursorAdapter(this, itemCursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER, "item_name",1,date,db);
    categoryLV.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            String s=((TextView)arg1.findViewById(R.id.category_name)).getText().toString();

            if(s.equalsIgnoreCase("all")){

                itemAdapter.changeCursor(null);
                itemAdapter.notifyDataSetChanged();
            }


            else{
            itemAdapter.changeCursor(db.getItemCursor(s));
            itemAdapter.notifyDataSetChanged();

            }



        }

    });



I get the error in the CursorAdapter class:

public void bindView(View view, Context context, Cursor cursor) {
    // TODO Auto-generated method stub
    if(mcolumn==CATEGORY_LV){
    TextView tv=(TextView)view.findViewById(R.id.category_name);
        tv.setText(mCursor.getString(mCursor.getColumnIndex(mColumnName)));
        tv.setTextSize(20); 
    }

    if(mcolumn==ITEM_LV){
        TextView item=(TextView)view.findViewById(R.id.item_name);
        TextView stock=(TextView)view.findViewById(R.id.stock_status);

                   // I get the staleDataException at this place
        String s=mCursor.getString(mCursor.getColumnIndex(mColumnName));
        Button drag=(Button)view.findViewById(R.id.drag_button);
        LinearLayout lv=(LinearLayout)view.findViewById(R.id.layout);
        drag.setFocusable(false);
        item.setText(s);
        item.setTextSize(20);
        stock.setText("Stock:"+mdb.getStockStatus(mdb.getItemCode(s), mDate));
        stock.setTextSize(15);
    }
}



One thing to note here is that I am using the same Adapter class to power both the listViews. However i dont think this can be a problem.

解决方案

what is mCursor? How did you create it? why don't you use the parameter

cursor

that from

public void bindView(View view, Context context, Cursor cursor)

?


这篇关于更改CursorAdapter的游标会产生staleDataException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆