我有试图用一个问题提出的CursorAdapter实现了CursorLoader在一起。 CursorAdapter的工作beatifully的时候,我可以为它提供通过游标一组静态数据的,但是当我试图用CursorLoader结合这一点,我得到一个问题nullpointers。 我已经固定下来的事实,当我喂适配器光标,它最初是空的(设置为null作为一个CursorLoader实现打交道时经常建议)。 适配器,在实例化,循环通过游标找出一个复选框处于什么状态,然后经过填充各种textviews和部件数据。 不幸的是,光标在实例空,只有当CursorLoader做是为了喂组数据。 我想弄清楚它是否在所有可能与CursorLoader一起使用的CursorAdapter,真的希望一些帮助。
这里是我的完整适配器:
public class ShopperListCursorAdapter extends CursorAdapter implements OnClickListener, LOG {
private LayoutInflater mInflater;
private GroceriesHelper mHelper;
private List<Boolean> mCheckedState;
public ShopperListCursorAdapter(Context context, Cursor cursor, GroceriesHelper helper, int flags) {
super(context, cursor, flags);
mHelper = helper;
mInflater = LayoutInflater.from(context);
for(mCheckedState = new ArrayList<Boolean>(); !cursor.isAfterLast(); cursor.moveToNext()) {
mCheckedState.add(cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_CHECKED)) != 0);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d(TAG, "newView");
View view = mInflater.inflate(R.layout.listview_row, null);
ViewHolder holder = new ViewHolder();
holder.amount = (TextView) view.findViewById(R.id.text_amount);
holder.unit = (TextView) view.findViewById(R.id.text_unit);
holder.item = (TextView) view.findViewById(R.id.text_item);
holder.checked = (CheckBox) view.findViewById(R.id.check_item);
holder.checked.setOnClickListener(this);
view.setTag(holder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(TAG, "bindView");
RowData data = new RowData();
data.id = cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_ID));
data.amount = cursor.getString(cursor.getColumnIndex(Groceries.COLUMN_AMOUNT));
data.unit = String.valueOf(cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_UNIT_ID)));
data.item = cursor.getString(cursor.getColumnIndex(Groceries.COLUMN_ITEM));
data.position = cursor.getPosition();
ViewHolder holder = (ViewHolder) view.getTag();
holder.amount.setText(data.amount);
holder.unit.setText(data.unit);
holder.item.setText(data.item);
holder.checked.setChecked(mCheckedState.get(data.position));
holder.checked.setTag(data);
}
@Override
public void onClick(View view) {
boolean visibility = ((CheckBox) view).isChecked();
RowData data = (RowData) view.getTag();
Log.d(TAG, "data: " + data.position);
mCheckedState.set(data.position, visibility);
mHelper.setChecked(data.id, visibility == true ? 1 : 0);
}
private static class ViewHolder {
TextView amount;
TextView unit;
TextView item;
CheckBox checked;
}
private static class RowData {
int id;
String amount;
String unit;
String item;
int position;
}
}