Skip to content

Commit

Permalink
More expts to get filter working (debug)
Browse files Browse the repository at this point in the history
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
mishrarohit committed Feb 24, 2012
1 parent 8f82960 commit 6906121
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 79 deletions.
11 changes: 7 additions & 4 deletions src/me/rohitmishra/groupbanker/FriendsDbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class FriendsDbAdapter {
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

Expand Down Expand Up @@ -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 ;
}

}
Expand Down
73 changes: 59 additions & 14 deletions src/me/rohitmishra/groupbanker/SelectFriends.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -31,6 +33,8 @@ public class SelectFriends extends ListActivity{
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.
Expand All @@ -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") ;

Expand Down Expand Up @@ -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 ;
}

}
});

Expand All @@ -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
Expand Down Expand Up @@ -175,13 +219,14 @@ 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) ;
}
};

@Override
protected void onDestroy() {
super.onDestroy();
filterText.removeTextChangedListener(filterTextWatcher);

}
}
75 changes: 14 additions & 61 deletions src/me/rohitmishra/groupbanker/SelectFriendsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand All @@ -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);
Expand All @@ -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() ;
Expand All @@ -120,28 +67,34 @@ 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() ;

This comment has been minimized.

Copy link
@mishrarohit

mishrarohit Feb 24, 2012

Author 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)

} 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());

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);
Expand Down

0 comments on commit 6906121

Please sign in to comment.