Android StaleDataException:重建ListView时访问关闭的游标错误
我的应用程序有一个Listview,它可以订购讲座列表。它们根据类型进行颜色编码,我使用的自定义适配器由以下代码调用-Android StaleDataException:重建ListView时访问关闭的游标错误,android,eclipse,android-listview,simplecursoradapter,Android,Eclipse,Android Listview,Simplecursoradapter,我的应用程序有一个Listview,它可以订购讲座列表。它们根据类型进行颜色编码,我使用的自定义适配器由以下代码调用- cursor = myDbHelper.getReadableDatabase().rawQuery(sql, null); startManagingCursor(cursor); adapter = new Lectures_Adapter(this,R.layout.menu_item,cursor,FROM,TO); menuList.se
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;
}
}
我有一个按钮,它显示一个对话框,该对话框提供了以四种不同方式重新排序Listview的选项。当他们选择了一个选项时,我使用以下代码对Listview重新排序-
Cursor newCursor = myDbHelper.getReadableDatabase().rawQuery(sqlStr, null);
adapter.changeCursor(newCursor);
adapter.notifyDataSetChanged();
这一切似乎都正常工作,除了我重新排序和调用上述代码时,当它运行getView时,我得到以下错误“StaleDataException:Access closed cursor”。这显然是因为光标已关闭,我做错了什么?当您使用getCursor获取当前光标值时,问题就解决了