Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More expts to get filter working (debug)

Tried a lot many more things. The problem still is that the getView in
selectFriendAdapter has a cursor which is closed when called after the
use of a filter.

No idea what we can do.
  • Loading branch information...
commit 69061216f90c7fdb1c4ff420e08a7c690ec123af 1 parent 8f82960
Rohit Mishra authored
11 src/me/rohitmishra/groupbanker/FriendsDbAdapter.java
View
@@ -19,8 +19,8 @@
public static final String KEY_NAME = "name";
public static final String KEY_IMAGEURI = "imageuri" ;
- private DatabaseHelper mDbHelper ;
- private SQLiteDatabase mDb ;
+ public DatabaseHelper mDbHelper ;
+ public SQLiteDatabase mDb ;
// Database creation SQL statement
@@ -120,9 +120,12 @@ public Cursor fetchAllFriends() throws SQLException {
}
public Cursor fetchFriendsWithSelection(String constraint) throws SQLException {
- Log.d(TAG, "fetchFriendsWithSelection called. constraint = " + constraint);
+ Log.d(TAG, "fetchFriendsWithSelection called. constraint = " + constraint +
+ " mDb = " + mDb );
String selection = KEY_NAME + " LIKE '%"+constraint+"%'";
- return mDb.query(TABLE_NAME,new String[] {KEY_ROWID, KEY_FBID, KEY_NAME, KEY_IMAGEURI}, selection, null, null, null, null);
+ Cursor c = mDb.query(TABLE_NAME,new String[] {KEY_ROWID, KEY_FBID, KEY_NAME, KEY_IMAGEURI}, selection, null, null, null, null);
+ Log.d(TAG, "Cursor = " + c + " has " + c.getCount()) ;
+ return c ;
}
}
73 src/me/rohitmishra/groupbanker/SelectFriends.java
View
@@ -7,6 +7,7 @@
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
+import android.database.sqlite.SQLiteQueryBuilder;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
@@ -17,6 +18,7 @@
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
+import android.widget.Filter;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
@@ -31,6 +33,8 @@
private static final String TAG = "SelectFriends";
private EditText filterText ;
private SelectFriendsAdapter adapter ;
+ private Cursor c;
+ private Context context ;
/* http://stackoverflow.com/questions/4188818/java-best-way-to-implement-a-dynamic-size-array-of-objects
* as per this discussion, arraylists are not good performance-wise compared to traditional functions.
@@ -51,17 +55,16 @@ public void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "onCreate called") ;
- checkedStates = new ArrayList<Boolean>() ;
- selectedIds = new HashSet<String>() ;
- selectedLines = new HashSet<Integer>() ;
+ context = this.getApplicationContext() ;
mDbHelper = new FriendsDbAdapter(this);
+
mDbHelper.open() ;
Log.v(TAG, "database opened") ;
- Cursor c = mDbHelper.fetchAllFriends();
- // startManagingCursor(c);
+ c = mDbHelper.fetchAllFriends();
+ startManagingCursor(c);
Log.v(TAG, "fetchAllFriends Over") ;
@@ -90,14 +93,29 @@ public void onCreate(Bundle savedInstanceState) {
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
+
+ try {
// Search for friends whose names begin with the specified letters.
Log.v(TAG, "runQuery Constraint = " + constraint) ;
//String selection = mDbHelper.KEY_NAME + " LIKE '%"+constraint+"%'";
-
- mDbHelper.open();
- Cursor c = mDbHelper.fetchFriendsWithSelection(
- (constraint != null ? constraint.toString() : null));
- return c;
+
+ mDbHelper = new FriendsDbAdapter(context);
+ mDbHelper.open();
+
+
+ Cursor cur = mDbHelper.fetchFriendsWithSelection(
+ (constraint != null ? constraint.toString() : null));
+
+ Log.d(TAG, "runQuery cursor is " + c + " has " + cur.getCount() + " rows");
+
+ // mDbHelper.close();
+ return cur;
+ } catch (Exception e) {
+ Log.e(TAG, "runQuery Exception = " + e);
+ Cursor cur = null ;
+ return cur ;
+ }
+
}
});
@@ -108,15 +126,41 @@ public Cursor runQuery(CharSequence constraint) {
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
-
- // listView.setOnItemClickListener(mListener);
+ listView.setFastScrollEnabled(true);
Button btn;
btn = (Button)findViewById(R.id.buttondone);
- mDbHelper.close();
+ // mDbHelper.close();
}
+
+ @Override
+ protected void onStop() {
+ try{
+ Log.d(TAG, "in onStop");
+ super.onStop();
+
+ if(this.adapter != null) {
+ this.adapter.getCursor().close();
+ this.adapter = null ;
+ }
+
+ if(this.c != null) {
+ this.c.close();
+ }
+
+ if(this.mDbHelper.mDb != null) {
+ this.mDbHelper.mDb.close();
+ }
+
+ if(this.mDbHelper != null) {
+ this.mDbHelper.close() ;
+ }
+ } catch (Exception error) {
+ Log.e(TAG, "We have an exception = " + error);
+ }
+ }
@Override
@@ -175,7 +219,7 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.v(TAG, "onTextChanged called. s = " + s);
- adapter.getFilter().filter(s);
+ adapter.getFilter().filter(s) ;
}
};
@@ -183,5 +227,6 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
protected void onDestroy() {
super.onDestroy();
filterText.removeTextChangedListener(filterTextWatcher);
+
}
}
75 src/me/rohitmishra/groupbanker/SelectFriendsAdapter.java
View
@@ -17,7 +17,7 @@
*/
public class SelectFriendsAdapter extends SimpleCursorAdapter {
- private static final String TAG = "FriendsAdapter";
+ private static final String TAG = "SelectFriendsAdapter";
private final Context context ;
private final String[] values ;
private final int[] to ;
@@ -41,58 +41,6 @@ public SelectFriendsAdapter(Context context, int layout, Cursor c,
Log.d(TAG, "At the end of the constructor") ;
}
-
- /*
- @Override
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- final View view=mInflater.inflate(layout,parent,false);
- return view;
- }
-
-
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- super.bindView(view, context, cursor);
-
- Log.d(TAG, "At the start of bindView." ) ;
- ViewHolder holder = (ViewHolder) view.getTag();
- if(holder == null) {
- Log.d(TAG, "holder = null");
- holder = new ViewHolder();
-
- holder.checkedText = (CheckedTextView) view.findViewById(R.id.text1) ;
- view.setTag(holder);
-
- }
-
- holder.checkedText.setText(cursor.getString(to[0]));
-
-
- // fill the checkedStates array with amount of bookmarks (prevent OutOfBounds Force close)
- if (cursor.moveToFirst()) {
- while (!cursor.isAfterLast()) {
- SelectFriends.checkedStates.add(false);
- cursor.moveToNext();
- }
- }
-
- String bookmarkID = cursor.getString(0);
- //CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle);
- if (SelectFriends.selectedIds.contains(new String(bookmarkID))) {
- holder.checkedText.setChecked(true);
- SelectFriends.selectedLines.add(object)
-
- } else {
- markedItem.setChecked(false);
- MainActivity.selectedLines.remove(pos);
- }
-
- Log.d(TAG, "At the end of rowView");
- return ;
-
- }
- */
-
@Override
public View getView(int position, View convertView, ViewGroup parent) {
super.getView(position, convertView, parent);
@@ -102,7 +50,6 @@ public View getView(int position, View convertView, ViewGroup parent) {
if(rowView == null) {
Log.d(TAG, "rowView = null");
try {
- //LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = mInflater.inflate(layout, parent, false);
Log.d(TAG, "rowView inflated. rowView = " + rowView);
ViewHolder viewHolder = new ViewHolder() ;
@@ -120,20 +67,26 @@ public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "getView cursor has " + cursor.getCount() + " rows. " +
"Cursor's current position is " + cursor.getPosition());
- // fill the checkedStates array with amount of bookmarks (prevent OutOfBounds Force close)
+
+
try {
+ cursor.moveToFirst() ;
Rohit Mishra Owner

This is where the error occurs when we are using the filter. It works fine when the initial list is being generated. The error message is: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, fbid, name, imageuri FROM friends)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ } catch (Exception e)
+ {
+ Log.e(TAG, "exception in try block on moveToFirst = " + e);
+ }
+
+ // fill the checkedStates array with amount of bookmarks (prevent OutOfBounds Force close)
+
if (cursor.moveToFirst()) {
Log.d(TAG, "moveToFirst worked");
while (!cursor.isAfterLast()) {
- Log.d(TAG, "Inside the checkSates loop. cursor position is " + cursor.getPosition());
SelectFriends.checkedStates.add(false);
cursor.moveToNext();
}
}
- } catch (Exception e)
- {
- Log.e(TAG, "exception in try block on 309 = " + e);
- }
+
+
cursor.moveToPosition(position);
Log.d(TAG, "Cursor position = " + cursor.getPosition());
@@ -141,7 +94,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
String bookmarkID = cursor.getString(cursor.getColumnIndex(FriendsDbAdapter.KEY_ROWID));
Log.d(TAG, "bookmarkID = " + bookmarkID );
- //CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle);
+
if (SelectFriends.selectedIds.contains(new String(bookmarkID))) {
holder.checkedText.setChecked(true);
SelectFriends.selectedLines.add(position);
Rohit Mishra

This is where the error occurs when we are using the filter. It works fine when the initial list is being generated. The error message is: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, fbid, name, imageuri FROM friends)

Please sign in to comment.
Something went wrong with that request. Please try again.