dimanche 12 avril 2015

Android SQLite: CursorAdapter gets stuck in constructor, possibly wrong Cursor

In my application, there is a database, and it also has a master/detail flow type list interface. The list has a CursorAdapter. When I try to initiate the CursorAdapter with a Cursor, it gets stuck in the constructor.


The implementation is the following:



public class PlaceAdapter extends CursorAdapter {

public PlaceAdapter(Context context, Cursor c) {
super(context, c, false);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.placerow, null);
bindView(row, context, cursor);
return row;
}

// UI elements
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Place from Cursor
Place place = PlaceDbLoader.getPlaceByCursor(cursor);

// UI elements...
ImageView imageViewIcon = ...

}


@Override
public Place getItem(int position) {
getCursor().moveToPosition(position);

return PlaceDbLoader.getPlaceByCursor(getCursor());
}

}


I am trying to use that Adapter to handle my list items. In the ListFragment, I have an AsyncTask to fetch all data and return the respectable Cursor:



//Async task for fetching data
private class GetAllTask extends AsyncTask<Void, Void, Cursor> {

private static final String TAG = "GetAllTask";

@Override
protected Cursor doInBackground(Void... params) {
try {
Log.d(TAG, "Doinbg0");

//IMPORTANT
Cursor result = dbLoader.fetchAll();

if (!isCancelled()) {
Log.d(TAG, "Doinbg1");
if(result==null)Log.d(TAG, "DoinbgNULL");
return result;
} else {
Log.d(TAG, "Cancelled, closing cursor");
if (result != null) {
result.close();
}
Log.d(TAG, "Doinbg2");
return null;
}
} catch (Exception e) {
Log.d(TAG, "Doinbg3");
return null;
}
}

@Override
protected void onPostExecute(Cursor result) {
super.onPostExecute(result);

Log.d(TAG, "Fetch completed, displaying cursor results!");
try {
if (adapter == null) {
Log.d(TAG, "onPostExec 0");

if(result==null) Log.d(TAG, "onPostExec NULL");

//IMPORTANT
adapter = new PlaceAdapter(getActivity()
.getApplicationContext(), result);

Log.d(TAG, "onPostExec 1");

setListAdapter(adapter);

Log.d(TAG, "onPostExec 2");
} else {
Log.d(TAG, "onPostExec 3");
adapter.changeCursor(result);
Log.d(TAG, "onPostExec 4");
}


getAllTask = null;
Log.d(TAG, "onPostExec done");
} catch (Exception e) {
}
}

}


The problem is that it gets stuck on



adapter = new PlaceAdapter(getActivity()
.getApplicationContext(), result);


According to the logcat, the Log messages are the following:



GetAllTask﹕ Doinbg0
GetAllTask﹕ Doinbg1
GetAllTask﹕ Fetch completed, displaying cursor results!
GetAllTask﹕ onPostExec 0


I suspect that something is wrong with the result or the database itself.


Here is the dbLoader.fetchAll(); implementation:



public Cursor fetchAll(){
// cursor for all records (where = null)
return mDb.query(
DbConstants.Place.DATABASE_TABLE,
new String[]{
DbConstants.Place.KEY_TITLE,
DbConstants.Place.KEY_DESCRIPTION,
DbConstants.Place.KEY_LAT,
DbConstants.Place.KEY_LNG,
DbConstants.Place.KEY_TYPE,
DbConstants.Place.KEY_RADIUS,
DbConstants.Place.KEY_ENABLED
}, null, null, null, null, DbConstants.Place.KEY_TITLE);
}


Here is the script for creating the table:



public static final String DATABASE_CREATE =
"create table if not exists "+DATABASE_TABLE+" ("
+ KEY_ROWID +" integer primary key autoincrement, "
+ KEY_TITLE + " text not null, "
+ KEY_DESCRIPTION +" text, "
+ KEY_LAT + " real not null, "
+ KEY_LNG +" real not null, "
+ KEY_TYPE +" text, "
+ KEY_RADIUS +" real not null, "
+ KEY_ENABLED +" text"
+ ");";


I would appreciate if someone could point me to the right direction on this one, I am completely clueless as to what is going wrong here.


Thank you!


Aucun commentaire:

Enregistrer un commentaire