我有一个使用 CursorLoader 填充的微调器。它被正确填充。但是 spinner.getAdapter().getCount() 一直返回 0。为什么会这样:(
我的微调班是
public class CategorySpinner extends Spinner implements LoaderManager.LoaderCallbacks<Cursor> {
Context myContext;
private SimpleCursorAdapter adapter;
public CategorySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
myContext=context;
}
public void fillData(int projectId) {
String[] from = new String[] { Category.CATEGORYNAME };
int[] to = new int[] { R.id.label1 };
int layout = R.layout.category_list_item;
Bundle bundle=new Bundle();
bundle.putInt("ID", projectId);
EditExpenseActivity mActivity=(EditExpenseActivity)myContext;
mActivity.getLoaderManager().initLoader(0, bundle,this);
adapter = new SimpleCursorAdapter(myContext, layout, null, from, to, 0);
adapter.setDropDownViewResource(R.layout.category_list_item);
this.setAdapter(adapter);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { Category.FULL_ID,Category.CATEGORYNAME };
Uri uri = Uri.parse(MyContentProvider.CATEGORY_LIST_PATH +args.getInt("ID"));
CursorLoader cursorLoader = new CursorLoader(myContext, uri, projection,null, null, null);
return cursorLoader;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// data is not available anymore, delete reference
adapter.swapCursor(null);
}
}
调用微调器的活动是 EditExpenseActivity
public class EditExpenseActivity extends Activity {
private Context myContext;
private CategorySpinner sp_Category;
private Intent intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_edit_expense);
intent=getIntent();
final int mProjectId=intent.getIntExtra("ID",0);
myContext = this;
sp_Category = (CategorySpinner) findViewById(R.id.input_category);
//fill spinner with categories of the project with Id mProjectId
sp_Category.fillData(mProjectId);
//so far so good .. spinner is populated perfectly
int i=sp_Category.getAdapter().getCount();
}
}
调试显示 i 值为 0。为什么???