SimpleCursorAdapater with Filtered Listview - 错误试图访问已关闭的CursorWindow

时间:2016-11-05 22:35:44

标签: android listview filter simplecursoradapter

我有一个包含3个标签的应用。在Tab1中我有一个带有光标适配器的列表视图,在Tab2和Tab3中我只有一些Textview。

当我使用代码过滤listview时会出现问题,接下来,我会在标签中随机点击。当我用Listview回到Tab1时,我得到了错误。

我跟踪问题,它位于setFilterQueryProvider。如果我摆脱过滤器一切正常。我做错了什么?

错误是:

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: net.techabout.medappointment, PID: 10716
                  android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.
                      at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:139)
                      at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                      at android.database.CursorWrapper.getString(CursorWrapper.java:137)
                      at net.techabout.medappointment.AgendaCursorAdapter.bindView(AgendaCursorAdapter.java:86)
                      at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:261)
                      at android.widget.AbsListView.obtainView(AbsListView.java:2346)

适配器类中发生错误:

   public void bindView(View view, Context context, final Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();

        String temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_AG_INDICE)); //**Error here**

我的片段是:

public class AgendaMedFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final String TAG = AgendaMedFragment.class.getSimpleName();
    // These variables will hold the date values later
    AgendaCursorAdapter adapter;
    ListView listView;
    private int mPosition = ListView.INVALID_POSITION;
    private static final String SELECTED_KEY = "selected_position";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.agendamedfrag, null);

        listView = (ListView) view.findViewById(R.id.list_cons);
        View v = (View) view.findViewById(R.id.empty_cons);
        listView.setEmptyView(v);
        adapter = new AgendaCursorAdapter(getActivity(), null,0);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                mPosition = position;
            }
        });

        if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) {
            mPosition = savedInstanceState.getInt(SELECTED_KEY);
        }

        EditText myFilter = (EditText) view.findViewById(R.id.myFilter);
        myFilter.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
                adapter.getFilter().filter(s.toString());
            }

            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                adapter.getFilter().filter(s.toString());
            }
        });


         adapter.setFilterQueryProvider(new FilterQueryProvider() {

            public Cursor runQuery(CharSequence constraint) {
                Cursor cursor = getFilteredNames(constraint != null ? constraint.toString() : null);
                return cursor;
            }

        });

        return view;
    }

    public Cursor getFilteredNames (CharSequence constraint)  {
        SQLiteDatabase mDB = DataProvider.getDB();
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(
                DataProvider.TABLE_AGENDA
        );

        String asColumnsToReturn[] = {"*"};

        if (constraint == null  ||  constraint.length () == 0)  {
            //  Return the full list
            return queryBuilder.query(mDB, asColumnsToReturn, null, null,
                    null, null, DataProvider.COL_ID + " DESC");
        }  else  {
            String value = "%"+constraint.toString()+"%";
            return mDB.query(DataProvider.TABLE_AGENDA, asColumnsToReturn, "nome_cliente like ? ", new String[]{value}, null, null, null);
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        CursorLoader loader = new CursorLoader(getActivity(),
                DataProvider.CONTENT_URI_AGENDA,
                null,
                null,
                null,
                DataProvider.COL_ID + " DESC");
        return loader;

    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getLoaderManager().getLoader(0)==null) {
            getLoaderManager().initLoader(0, null, this);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }


    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        adapter.swapCursor(data);
        if (mPosition != ListView.INVALID_POSITION) {
            listView.smoothScrollToPosition(mPosition);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        if (mPosition != ListView.INVALID_POSITION) {
            outState.putInt(SELECTED_KEY, mPosition);
        }
        super.onSaveInstanceState(outState);
    }
}

我的适配器是:

public class AgendaCursorAdapter  extends CursorAdapter {

    public static class ViewHolder {
        public final Button butDesmarcar;
        public final Button butVermapa;
        public final TextView mProtocolo;
        public final TextView mNomeCliente;
        public final TextView mNomeHosp;
        public final TextView mNomeMed;
        public final TextView mNomeConv;
        public final TextView mNomePlano;
        public final TextView mDatamarcada;
        public final TextView mHoraMarcada;

        public ViewHolder(View view) {
            butDesmarcar = (Button) view.findViewById(R.id.butdesmarcar);
            butVermapa = (Button) view.findViewById(R.id.but_ender_hosp_cons);
            mProtocolo = (TextView) view.findViewById(R.id.tvindice);
            mNomeCliente = (TextView) view.findViewById(R.id.tvnomecliente);
            mNomeHosp = (TextView) view.findViewById(R.id.tvnomehosp);
            mNomeMed = (TextView) view.findViewById(R.id.tvnomemed);
            mNomeConv= (TextView) view.findViewById(R.id.tvnomeconv);
            mNomePlano= (TextView) view.findViewById(R.id.tvnomeplan);
            mDatamarcada= (TextView) view.findViewById(R.id.tvdatamarcada);
            mHoraMarcada= (TextView) view.findViewById(R.id.tvhoramarcada);
        }
    }

    public AgendaCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int layoutId = R.layout.singlerowagenda;
        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

        return view;
    }

    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();

        String temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_AG_INDICE));
        viewHolder.mProtocolo.setText(temp);

        temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_AG_NOME_CLIENTE));
        viewHolder.mNomeCliente.setText(temp);

        temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_AG_NOME_HOSP));
        viewHolder.mNomeHosp.setText(temp);

        temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_AG_NOME_MED));
        viewHolder.mNomeMed.setText(temp);

        temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_AG_NOME_CONV));
        viewHolder.mNomeConv.setText(temp);

        temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_AG_NOME_PLANO));
        viewHolder.mNomePlano.setText(temp);

        temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_AG_DATA_MARCADA));
        viewHolder.mDatamarcada.setText(temp);

        temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_AG_HORA_MARCADA));
        viewHolder.mHoraMarcada.setText(temp);
    }
}

0 个答案:

没有答案