FIXED - filter list with SimpleCursorAdapter db problem

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

FIXED - filter list with SimpleCursorAdapter db problem

Postby alon24 » Thu Aug 04, 2011 3:10 pm

I have a db, and a list that shows code from that db.
I want in my view to filter said results from the db.
I am getting an error all the time:

Code: Select all
E/AndroidRuntime(31881): java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, serviceTypename, category_desc, category_type FROM citycategories)
E/AndroidRuntime(31881):    at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
E/AndroidRuntime(31881):    at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:67)
E/AndroidRuntime(31881):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:287)
E/AndroidRuntime(31881):    at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:259)
E/AndroidRuntime(31881):    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:188)
E/AndroidRuntime(31881):    at com.test.cursor.utils.CategoryCursorAdapter.getView(CategoryCursorAdapter.java:53)
E/AndroidRuntime(31881):    at android.widget.AbsListView.obtainView(AbsListView.java:1409)
E/AndroidRuntime(31881):    at android.widget.ListView.makeAndAddView(ListView.java:1745)
E/AndroidRuntime(31881):    at android.widget.ListView.fillSpecific(ListView.java:1290)
E/AndroidRuntime(31881):    at android.widget.ListView.layoutChildren(ListView.java:1576)
E/AndroidRuntime(31881):    at android.widget.AbsListView.onLayout(AbsListView.java:1260)
E/AndroidRuntime(31881):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(31881):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
E/AndroidRuntime(31881):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
E/AndroidRuntime(31881):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
E/AndroidRuntime(31881):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(31881):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
E/AndroidRuntime(31881):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(31881):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
E/AndroidRuntime(31881):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
E/AndroidRuntime(31881):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
E/AndroidRuntime(31881):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(31881):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
E/AndroidRuntime(31881):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(31881):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
E/AndroidRuntime(31881):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
E/AndroidRuntime(31881):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(31881):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(31881):    at android.app.ActivityThread.main(ActivityThread.java:3837)
E/AndroidRuntime(31881):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(31881):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(31881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(31881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(31881):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  127):   Force finishing activity com.test.cursor/.TestCursorDbActivity



What am I doing wrong? I tried to follow many tutorials but still no go.
Here is my code:

CategoryCursorAdapter:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CategoryCursorAdapter extends SimpleCursorAdapter implements Filterable
  2. {
  3.  
  4.         private Cursor c;
  5.         private LayoutInflater inflater;
  6.         private Context ctx;
  7.  
  8.         public CategoryCursorAdapter(Context context, int layout, Cursor c,
  9.                         String[] from, int[] to)
  10.         {
  11.                 super(context, layout, c, from, to);
  12.                 ctx = context;
  13.                 this.c = c;
  14.                 inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  15.         }
  16.  
  17.         public View getView(int pos, View inView, ViewGroup parent)
  18.         {
  19.                 // A ViewHolder keeps references to children views to avoid unneccessary calls
  20.         // to findViewById() on each row.
  21.         ViewHolder holder;
  22.                 if (inView == null)
  23.                 {
  24.                         inView = inflater.inflate(R.layout.list_rowlayout, null);
  25.                        
  26.                         holder = new ViewHolder();
  27.                         holder.serviceTypeName = (TextView) inView.findViewById(R.id.serviceTypeName);
  28.                         inView.setTag(holder);
  29.                 }
  30.                 else
  31.                 {
  32.                         // Get the ViewHolder back to get fast access to the TextView
  33.             // and the ImageView.
  34.             holder = (ViewHolder) inView.getTag();
  35.                 }
  36.                
  37.                 this.c.moveToPosition(pos);
  38.                 String serviceTypeName =
  39.                                 this.c.getString(this.c
  40.                                                 .getColumnIndex(TestCursorDbAdapter.KEY_SERVICE_TYPE_NAME));
  41.                 holder.serviceTypeName.setText(serviceTypeName);
  42.                
  43.                 return (inView);
  44.         }
  45.        
  46.         public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
  47.         if (getFilterQueryProvider() != null) {
  48.             return getFilterQueryProvider().runQuery(constraint);
  49.         }
  50.        
  51.         Cursor cursor =  ((TestCursorApplication) ctx.getApplicationContext())
  52.                 .getDBAdapter().fetchFilteredCategories(constraint != null ? constraint.toString() : null);
  53.  
  54.         return cursor;
  55.     }
  56.  
  57.         static class ViewHolder {
  58.                 public TextView serviceTypeName;
  59.         }
  60.  
  61. }
  62.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


TestCursorDbAdapter:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestCursorDbAdapter {
  2.  
  3.         // Database fields
  4.         public static final String KEY_ROWID = "_id";
  5.         public static final String KEY_SERVICE_TYPE_NAME = "serviceTypename";
  6.         public static final String KEY_CATEGORY_DESCRIPTION = "category_desc";
  7.         public static final String KEY_CATEGORY_TYPE = "category_type";
  8.        
  9.         private static final String DATABASE_TABLE_CATEGORIES = "citycategories";
  10.        
  11.         private Context context;
  12.         private SQLiteDatabase database;
  13.         private TestCursorDatabaseHelper dbHelper;
  14.  
  15.         public TestCursorDbAdapter(Context context) {
  16.                 this.context = context;
  17.         }
  18.  
  19.         public TestCursorDbAdapter open() throws SQLException {
  20.                 dbHelper = new TestCursorDatabaseHelper(context);
  21.                 database = dbHelper.getWritableDatabase();
  22.                 return this;
  23.         }
  24.  
  25.         public void close() {
  26.                 dbHelper.close();
  27.         }
  28.  
  29.        
  30.         /**
  31.          * Deletes all categories
  32.          */
  33.         public boolean deleteCategories() {
  34.                 return database.delete(DATABASE_TABLE_CATEGORIES, "", null) > 0;
  35.         }
  36.        
  37.         public long createCat(String catType, String catName, String catDesc) {
  38.                 ContentValues initialValues = createContentValuesCategories(catType, catName, catDesc);
  39.  
  40.                 return database.insert(DATABASE_TABLE_CATEGORIES, null, initialValues);
  41.         }
  42.        
  43.         public Cursor fetchCat(long rowId) throws SQLException {
  44.                 Cursor mCursor = dbHelper.getWritableDatabase().query(true, DATABASE_TABLE_CATEGORIES, new String[] {
  45.                                 KEY_ROWID, KEY_SERVICE_TYPE_NAME, KEY_CATEGORY_DESCRIPTION },
  46.                                 KEY_ROWID + "=" + rowId, null, null, null, null, null);
  47.                 if (mCursor != null) {
  48.                         mCursor.moveToFirst();
  49.                 }
  50.                 return mCursor;
  51.         }
  52.        
  53.         public Cursor fetchAllCategories()
  54.         {
  55.                 Cursor c =  database.query(DATABASE_TABLE_CATEGORIES, new String[] { KEY_ROWID,
  56.                                 KEY_SERVICE_TYPE_NAME, KEY_CATEGORY_DESCRIPTION, KEY_CATEGORY_TYPE}, null, null, null,
  57.                                 null, null);
  58.                
  59.                 Log.d("db", ">>>>>categories count = " +c.getCount());
  60.                 return c;
  61.         }
  62.        
  63.         public Cursor fetchFilteredCategories(String constraint) throws SQLException
  64.         {
  65.                 Cursor c = database.query(DATABASE_TABLE_CATEGORIES, new String[] {
  66.                                 KEY_ROWID, KEY_SERVICE_TYPE_NAME, KEY_CATEGORY_DESCRIPTION,
  67.                                 KEY_CATEGORY_TYPE }, KEY_SERVICE_TYPE_NAME + " LIKE '%" + constraint + "%'", null,
  68.                                 null, null, null);
  69.                
  70.                 Log.d("db", ">>>>>filtered count = " +c.getCount());
  71.                 return c;
  72.         }
  73.  
  74.         private ContentValues createContentValuesCategories(String serviceTypeType, String catName, String categoryDesc) {
  75.                 ContentValues values = new ContentValues();
  76.                 values.put(KEY_CATEGORY_TYPE, serviceTypeType);
  77.                 values.put(KEY_SERVICE_TYPE_NAME, catName);
  78.                 values.put(KEY_CATEGORY_DESCRIPTION, categoryDesc);
  79.                 return values;
  80.         }
  81.  
  82. }
  83.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


TestCursorDbActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestCursorDbActivity extends Activity
  2. {
  3.         /** Called when the activity is first created. */
  4.  
  5.         TestCursorApplication application;
  6.         private ListView listView;
  7.  
  8.         @Override
  9.         public void onCreate(Bundle savedInstanceState)
  10.         {
  11.                 super.onCreate(savedInstanceState);
  12.                 setContentView(R.layout.main);
  13.  
  14.                 application = (TestCursorApplication) getApplication();
  15.  
  16.                 new DBAsyncTasks(this).execute(DBAsyncTasks.GET_CATEGORIES_FROM_SERVER);
  17.  
  18.                 // for (int i = 0; i < 50; i++)
  19.                 // {
  20.                 // mAdapter.addItem("item " + i);
  21.                 // }
  22.  
  23.                 listView = (ListView) findViewById(android.R.id.list);
  24.                 listView.setDividerHeight(2);
  25.                 // listView.setOnItemClickListener(this);
  26.                 listView.setTextFilterEnabled(true);
  27.                 fillData();
  28.  
  29.                 EditText text = (EditText) findViewById(R.id.searchText);
  30.                 text.addTextChangedListener(filterTextWatcher);
  31.         }
  32.  
  33.         private TextWatcher filterTextWatcher = new TextWatcher()
  34.         {
  35.  
  36.                 public void afterTextChanged(Editable s)
  37.                 {
  38.                 }
  39.  
  40.                 public void beforeTextChanged(CharSequence s, int start, int count,
  41.                                 int after)
  42.                 {
  43.                 }
  44.  
  45.                 public void onTextChanged(CharSequence s, int start, int before,
  46.                                 int count)
  47.                 {
  48.                         ((CategoryCursorAdapter) listView.getAdapter()).getFilter().filter(
  49.                                         s);
  50.                 }
  51.  
  52.         };
  53.  
  54.         public void fillData()
  55.         {
  56.                 String[] from =
  57.                                 new String[] { TestCursorDbAdapter.KEY_SERVICE_TYPE_NAME,
  58.                                                 TestCursorDbAdapter.KEY_CATEGORY_DESCRIPTION };
  59.                 int[] to = new int[] { R.id.serviceTypeName };
  60.  
  61.                 Cursor cursor = application.getDBAdapter().fetchAllCategories();
  62.                 startManagingCursor(cursor);
  63.  
  64.                 CategoryCursorAdapter categoryAdapter =
  65.                                 new CategoryCursorAdapter(this, R.layout.list_rowlayout,
  66.                                                 cursor, from, to);
  67.                 listView.setAdapter(categoryAdapter);
  68.  
  69.                 // Set the FilterQueryProvider, to run queries for choices
  70.                 // that match the specified input.
  71.                 categoryAdapter.setFilterQueryProvider(new FilterQueryProvider()
  72.                 {
  73.                         public Cursor runQuery(CharSequence constraint)
  74.                         {
  75.                                 // Search for states whose names begin with the specified
  76.                                 // letters.
  77.                                 Cursor cursor =
  78.                                         application.getDBAdapter()
  79.                                                                 .fetchFilteredCategories(
  80.                                                                                 (constraint != null ? constraint
  81.                                                                                                 .toString() : null));
  82.                                 return cursor;
  83.                         }
  84.                 });
  85.         }
  86. }
  87.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


TestCursorApplication:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestCursorApplication extends Application
  2. {
  3.         private TestCursorDbAdapter dbHelper;
  4.        
  5.  
  6.         @Override
  7.         public void onCreate()
  8.         {
  9.                 dbHelper = new TestCursorDbAdapter(this);
  10.             dbHelper.open();
  11.         }
  12.        
  13.         public TestCursorDbAdapter getDBAdapter()
  14.         {
  15.                 return dbHelper;
  16.         }
  17.        
  18. }
  19.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Attachments
TestCursorDb.zip
(66.67 KiB) Downloaded 113 times
Last edited by alon24 on Sun Aug 07, 2011 7:45 am, edited 1 time in total.
alon24
Freshman
Freshman
 
Posts: 2
Joined: Thu Aug 04, 2011 2:54 pm

Top

Re: filter list with SimpleCursorAdapter db problem

Postby alon24 » Sun Aug 07, 2011 7:45 am

I found the problem and fixed it:

In CategoryCursorAdapter change to:
this.c .... to:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Cursor cursor = getCursor();
  2.       cursor.moveToPosition(pos);
  3.       String serviceTypeName =
  4.          cursor.getString(this.c
  5.                   .getColumnIndex(TestCursorDbAdapter.KEY_SERVICE_TYPE_NAME));
  6.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Explenation:

I did not get the new cursor every time I did get view, that in turn threw an exception after the filter was done because it had the old cursor in it.

getCursor fixed all.
alon24
Freshman
Freshman
 
Posts: 2
Joined: Thu Aug 04, 2011 2:54 pm

Re: FIXED - filter list with SimpleCursorAdapter db problem

Postby klwinkel » Mon May 13, 2013 2:21 pm

Thank you, solved my problem too!
klwinkel
Once Poster
Once Poster
 
Posts: 1
Joined: Mon May 13, 2013 2:18 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests