不会调用newView和bindView

时间:2014-12-20 17:42:05

标签: android android-cursoradapter android-cursorloader

我正在尝试使用CursorAdapterListActivity内设置自定义CursorLoader,但不幸的是,被覆盖的方法newViewbindView永远不会被调用。我只能看到ListViewSimpleCursorAdapter。我试过一切都没有效果。这是我的代码:

我的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);
}

}

我的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;

}

}

这是行项目的xml:

<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" />

在我恢复SimpleCursorAdapter解决方案之前的任何建议?提前致谢

1 个答案:

答案 0 :(得分:0)

经过数小时的头部刮擦,我终于解开了这个谜团: 在我的自定义CursorAdapter实现中,我有以下覆盖方法:

@Override
public int getCount() {
    return mBgRecords.size();
}

其中mBgRecords是记录的ArrayList,我认为在光标绑定到适配器时它是null。所以,我用以下内容替换它:

@Override
public int getCount() {
    if (getCursor() == null) {
        return 0;
    }
    return super.getCount();
}

一切都恢复正常