2012-03-05 1 views
0

私のアプリケーションには、リストビューのリストがあります。StaleDataException:リストビューの再構築時にカーソルが閉じたときにエラーが発生しました

cursor = myDbHelper.getReadableDatabase().rawQuery(sql, null);  
startManagingCursor(cursor); 
adapter = new Lectures_Adapter(this,R.layout.menu_item,cursor,FROM,TO);   
menuList.setAdapter(adapter); 

私のカスタム・アダプターのコードがある - - 彼らは、色の種類に応じてコード化されて、私はこれを行うために使用するカスタムアダプタは、次のコードによって呼び出され

public class Lectures_Adapter extends SimpleCursorAdapter { 
    private Context appContext; 
    private int layout; 
    private Cursor mycursor; 

    public Lectures_Adapter(Context context, int layout, Cursor c, String[] from,int[] to) { 
      super(context, layout, c, from, to); 
      this.appContext=context; 
      this.layout=layout; 
      this.mycursor=c;    
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent)  
    { 
      View view = super.getView(position, convertView, parent); 
      try {    
      if (position > 0) 
      {    
       RelativeLayout rowFill = (RelativeLayout) convertView.findViewById(R.id.rowFill); 
       String title = mycursor.getString(1);     
       int myColor = 0; 
       int myPos = title.indexOf("Nursing"); 
       int myPos2 = title.indexOf("Masterclass"); 
       if (myPos >= 0) 
       { 
        myColor = Color.parseColor("#99FF66"); 
       } 
       else if (myPos2 >= 0) 
       { 
        myColor = Color.parseColor("#FF99FF"); 
       } 
       else 
       { 
        myColor = Color.parseColor("#FFFF66"); 
       } 
       convertView.findViewById(R.id.rowFill).setBackgroundColor(myColor);     
       }   
      }catch(Exception e) { 

      } 

      if (convertView == null) { 
       LayoutInflater inflator = (LayoutInflater) this.appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflator.inflate(this.layout,null); 
      } else { 
       convertView = (View) convertView; 
      } 
      return view; 
     } 

    } 

私がボタンを持っていますリストビューを4つの異なる方法で並べ替えるオプションを提供するダイアログボックスを表示します。彼らは、次のコードを使用してオプション、私は再オーダーリストビューを選択した場合は -

 Cursor newCursor = myDbHelper.getReadableDatabase().rawQuery(sqlStr, null); 
     adapter.changeCursor(newCursor);     
     adapter.notifyDataSetChanged(); 

これを、すべてのは、それがgetViewメソッドを実行したときに私が得る、I再注文と上記のコードが呼び出されたときを除いて、[OK]を動作しているようです次のエラー 'StaleDataException:Access closed cursor'です。これは確かにカーソルが閉じているので、何が間違っているのですか?

答えて

0

getCursor()を使用して現在のカーソル値を取得すると、この問題は解決されます。

関連する問題