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 であることを示しています。