开发者

Problem with ListView, Custom SimpleCursorAdapter & FilterQueryProvider

开发者 https://www.devze.com 2023-02-25 04:19 出处:网络
In short i have my listview being filtered with a standard SimpleQueryAdapter but when i try applying my custom SimplyQueryAdapter implementation it breaks with an exception and i can\'t figure out wh

In short i have my listview being filtered with a standard SimpleQueryAdapter but when i try applying my custom SimplyQueryAdapter implementation it breaks with an exception and i can't figure out why.

At a cut down level I have an application which lists items. I've been trying to implement a way of filtering those results "dynamically" via what the user enters in a related EditView.

For testing purposes my code simply has a button at the top of the list, when i click the button it assigns a FilterQueryProvider and provides a hardcoded constraint string. This emulates a person typing something like "ob" into the textfield.

The problem is when i try running against a list populated via my custom SimpleQueryAdapter. Every time i click the button it reduces the list but displays the incorrect item and also raises a "java.lang.IllegalStateException". For Example, I might search for "ob" - instead of showing "robert" it shows "andrew". For now what is more worrying is the exception.

Here is the full exception :

04-15 12:23:25.099: ERROR/2130968577MySimpleCursorAdapter.getView(577): java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, idSite, idSiteUser, idUser, siteName, siteType, siteUrlIcon, dateModified, siteStatus FROM site WHERE idUser=14742 ORDER BY siteName DESC)

The reason i suspect something going wrong with my Queryadapter is If i try switching to using a non custom SimpleQueryAdapter it works perfectly. but i do need to implement a custom adapter as i'm using lazy icon loading etc.

I'm doing the filtering this way as I want it to search on the table using a WHERE LIKE ('%blah%') ie, contains, rather than a filter. I found the idea via this thread: FilterQueryProvider, filter and ListView

Based on the exception it seems my cursor is being closed but i'm not calling close anywhere and the adapter should be managing automatically via StartManagingCursor. Am i doing something wrong regarding the database connection and cursor? Does my use of FilterQueryProvider look right?

Here is the code. Its restructured for conciseness

here is my main ListActivity:

        public class ChannelListContainer extends ListActivity {
    Context context;
    private MyDBAdapter db;
    private int dummyUserId = 14742;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this.getApplicationContext();

        setContentView(R.layout.channellist);    

        /* open database connection */
        db = new MyDBAdapter(context); 
        db.open();        

        Cursor c = db.fetchAllSites(dummyUserId);
        startManagingCursor(c);

        String[] from = new String[] {DBAdapter.KEY_SITE_NAME}; 
        int[] to = new int[] {R.id.channel_name};

        /// Code using my custom SimpleCursorAdapter (does not work). By swapping between my addapter and the standard cursor adapter i can make it work/not work
        MySimpleCursorAdapter channels = new MySimpleCursorAdapter(this, R.layout.list_item, c, from, to);
        setListAdapter(channels);

    /// Code not using my custom SimpleCursorAdapter (works)
        //SimpleCursorAdapter channels = new SimpleCursorAdapter(this, R.layout.list_item, c, from, to);
        //setListAdapter(channels);
    }       

    private void filterList(CharSequence _constraint) { 
        /// Code using my custom SimpleCursorAdapter (does not work)
        //final MySimpleCursorAdapter channels = (MySimpleCursorAdapter) getListAdapter();

        /// Code not using my custom SimpleCursorAdapter (works)
        final SimpleCursorAdapter channels = (SimpleCursorAdapter) getListAdapter();


        final Cursor oldCursor = channels.getCursor();
        Filter filter;

        channels.setFilterQueryProvider(filterQueryProvider);
        filter = channels.getFilter();
        filter.filter(_constraint, new FilterListener() {
            public void onFilterComplete(int count) {
                stopManagingCursor(oldCursor);
                final Cursor newCursor = channels.getCursor();
                startManagingCursor(newCursor);

                if (oldCursor != null && !oldCursor.isClosed()) {
                    oldCursor.close();
                }
            }

        });
    }

    pr开发者_开发百科ivate FilterQueryProvider filterQueryProvider = new FilterQueryProvider() {
        public Cursor runQuery(CharSequence _constraint) {

            db = new MyDBAdapter(context);
            db.open();
            Cursor c = db.fetchAllSitesByName(dummyUserId, (String) _constraint);

            return c;   
        }
    };

    // filter the list when button called. Demo to filter list to "To Do" item.
    public void onClick(View _view) {
        filterList("To Do");
    }   
}

Here is my Custom SimpleCursorAdapter:

public class MySimpleCursorAdapter extends SimpleCursorAdapter{
    String[] from;
    Cursor c; 
    private static LayoutInflater inflater=null;

    int INDEX_ROWID = 0; 
    int INDEX_IDSITE = 1;
    int INDEX_IDSITEUSER = 2;
    int INDEX_IDUSER = 3;
    int INDEX_SITE_NAME = 4;
    int INDEX_SITE_TYPE = 5;
    int INDEX_SITE_URL_ICON = 6;
    int INDEX_DATE_MODIFIED = 7;

    public MySimpleCursorAdapter(Activity activity, int layout, Cursor cursor, String[] from, int[] to) {

        super(activity.getApplicationContext(), layout, cursor, from, to);
        this.from = from;
        this.c = cursor;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder{
        public TextView channel_name;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;   

        try {
            ViewHolder holder;
            if (this.c.moveToPosition(position)) {
                if(convertView==null){
                    vi = inflater.inflate(R.layout.list_item, null);
                    holder=new ViewHolder(); 
                    holder.channel_name=(TextView)vi.findViewById(R.id.channel_name);
                    vi.setTag(holder); 
                }
                else {
                    holder=(ViewHolder)vi.getTag();
                }                 

                holder.channel_name.setText(c.getString(INDEX_SITE_NAME));

                return vi;  
            }

        } catch (Exception e) {
            Log.e(R.string.app_name + "MySimpleCursorAdapter.getView",e.toString());
        }
        return vi;  
    }
}

database open:

public MyDBAdapter(Context _context) {
    context = _context;
    dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public MyDBAdapter open() throws SQLException {
    db = dbHelper.getWritableDatabase();
    return this;
}

Any help would be greatly appreciated.

Thanks, Rob

0

精彩评论

暂无评论...
验证码 换一张
取 消