I am trying to setup a custom CursorAdapter
inside a ListActivity
with a CursorLoader
, but unfortunately the overridden methods newView
and bindView
never get called. I can only see the ListView
items with a SimpleCursorAdapter
. I have tried everything to no effect. Here's my code:
My ListActivity:
public class BgListActivity extends ListActivity implements
LoaderCallbacks<Cursor>{
private static String TAG = "BG-ListActivity";
private BgAdapter mCursorAdapter;
//private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView();
mCursorAdapter = new BgAdapter(this, null, 0);
setListAdapter(mCursorAdapter);
getLoaderManager().initLoader(0, null, this);
/*adapter = new SimpleCursorAdapter(this, R.layout.bg_list, null,
new String [] {BgContract.DATE, BgContract.TIME_OF_DAY,
BgContract.BG_MEASUREMENT}, new int [] {R.id.date_textview,
R.id.timeofday_textview, R.id.measurement_textview}, 0);*/
Log.i(TAG, "BgAdapter and loader initialized");
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.i(TAG, "Entered onCreateLoader() callback method");
final String [] PROJECTION = new String [] {BgContract._ID, BgContract.DATE,
BgContract.TIME_OF_DAY, BgContract.BG_MEASUREMENT};
return new CursorLoader(this, BgContract.CONTENT_URI, PROJECTION,
null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> newLoader, Cursor newCursor) {
Log.i(TAG, "Entered onLoadFinished() callback method");
mCursorAdapter.swapCursor(newCursor);
}
@Override
public void onLoaderReset(Loader<Cursor> newLoader) {
Log.i(TAG, "Entered onLoaderReset() callback method");
mCursorAdapter.swapCursor(null);
}
}
My CursorAdapter:
public class BgAdapter extends CursorAdapter {
private static LayoutInflater sLayoutInflater = null;
private Context mContext;
public BgAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
mContext = context;
sLayoutInflater = LayoutInflater.from(mContext);
Log.i(TAG,"BgAdapter constructor created " + mContext.toString() +
" " + sLayoutInflater.toString() + " cursor == null? " + (cursor==null));
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.date.setText(cursor.getString(cursor.
getColumnIndex(BgContract.DATE)));
holder.timeOfDay.setText(cursor.getString(cursor.
getColumnIndex(BgContract.TIME_OF_DAY)));
holder.bGMeasurement.setText(Float.toString(cursor.getFloat(cursor.
getColumnIndex(BgContract.BG_MEASUREMENT))));
Log.i(TAG, "Views loaded");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View newView;
ViewHolder holder = new ViewHolder();
newView = sLayoutInflater.inflate(R.layout.bg_list, parent,
false);
holder.date = (TextView) newView.findViewById(R.id.date_textview);
holder.timeOfDay = (TextView) newView.findViewById(R.id.timeofday_textview);
holder.bGMeasurement = (TextView) newView.findViewById(R.id.measurement_textview);
newView.setTag(holder);
Log.i(TAG, "NewView setup!");
return newView;
}
}
And here is the xml for the row items:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/date_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="5dp"
android:singleLine="true"
android:textSize="12sp" />
<TextView
android:id="@+id/timeofday_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="middle"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:textSize="12sp" />
<TextView
android:id="@+id/measurement_textview"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="right"
android:padding="5dp"
android:textSize="12sp" />
Any suggestions before I revert to the SimpleCursorAdapter
solution? Thanks in advance
After hours of head scratching I finally solved the mystery: In my custom CursorAdapter implementation I had the following overriden method:
@Override
public int getCount() {
return mBgRecords.size();
}
where mBgRecords is an ArrayList of records, which I suppose was null at the time the cursor was bound to the adapter. So, I replaced it with the following:
@Override
public int getCount() {
if (getCursor() == null) {
return 0;
}
return super.getCount();
}
and everything is back to normal