내 코드에서 스피너 드롭 다운을 사용하고 있는데, 동적으로 채워지는 값이 4 ~ 5 개 있습니다. 예를 들어 "사과"를 기본값으로 설정하고 드롭 다운에서 "주황색"을 선택하고 화면을 세로에서 가로로 회전하면 연결된보기와 함께 기본 "사과"로 돌아갑니다. "주황색"을 선택하고 가로로 회전 할 때 선택한 값을 채우거나 동일한 선택된 상태를 유지하고보기를 유지하도록 상태를 저장하는 방법 선택한 값에 해당하는 세로 모드에서 선택한보기를 그대로 / 채 웁니다. 동일한 용도로 사용하는 어댑터 코드는 다음과 같습니다.
public class MarketsSpinnerAdapter extends CustomRowAdapter<AdapterRow> {
private List<AdapterRow> mRenderList;
public MarketsSpinnerAdapter(final Context context, final List<AdapterRow> renderList) {
super(context);
mRenderList = new ArrayList<AdapterRow>();
mRenderList.addAll(renderList);
}
@Override
protected void setEntries(final List<AdapterRow> renderList) {
mRenderList = renderList;
}
@Override
protected List<AdapterRow> getEntries() {
return mRenderList;
}
@Override
public View getDropDownView(final int position, final View convertView, final ViewGroup parent) {
return getEntries().get(position).getDropDownView(mContext, convertView);
}
}
각 조각에서 해당 사용법 :
private void populateCategoryRows(final Cursor cursor) {
mCategories.clear();
mAllCategories.clear();
cursor.moveToPosition(-1);
Map<String, String> categoryParentNames = new HashMap<String, String>();
int selectedPosition = 0;
String previousHeader = "";
String previousAllHeader = "";
while (cursor.moveToNext()) {
final int categoryLevel = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.LEVEL));
final String categoryName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.NAME));
final String categoryDisplayName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.DISPLAY_NAME));
if (categoryLevel == 1) {
categoryParentNames.put(categoryName, categoryDisplayName);
}
}
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
final int categoryLevel = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.LEVEL));
final boolean categoryIsDefault = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.IS_DEFAULT)) == 1;
final boolean categoryIsSelected = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.IS_SELECTED)) == 1;
final String categoryParent = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.PARENT));
final String categoryName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.NAME));
final String categoryDisplayName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.DISPLAY_NAME));
if (categoryLevel == 2 ) {
String categoryParentDisplayName = categoryParentNames.get(categoryParent);
if (!categoryParent.equals(previousHeader)) {
if (categoryIsSelected) {
mCategories.add(new CategoryHeader(categoryParentDisplayName));
previousHeader = categoryParent;
}
}
if (!categoryParent.equals(previousAllHeader)) {
mAllCategories.add(new CategoryHeader(categoryParentDisplayName));
previousAllHeader = categoryParent;
}
if (categoryIsSelected) {
mCategories.add(new SpinnerMarketCategoryRow(categoryName, categoryDisplayName, categoryParent));
}
mAllCategories.add(new MarketsCategoryCheckableRow(categoryName, categoryDisplayName, categoryIsSelected, categoryIsDefault));
if(categoryIsDefault){
selectedPosition = mCategories.size()-1;
}
}
}
mSpinnerAdapter = new MarketsSpinnerAdapter(Application.getAppContext(), mCategories);
headerView.setSpinnerAdapter(mSpinnerAdapter);
headerView.setSpinnerSelectedItemPosition(selectedPosition);
}
if (selectedItem instanceof SpinnerMarketCategoryRow) {
selectedCategory = (SpinnerMarketCategoryRow) mSpinnerAdapter.getItem(position);
} else {
if (mSpinnerAdapter.getCount() - 1 >= position + 1) {
selectedCategory = (SpinnerMarketCategoryRow) mSpinnerAdapter.getItem(position + 1);
} else {
selectedCategory = (SpinnerMarketCategoryRow) mSpinnerAdapter.getItem(position - 1);
}
}
final MarketsFragment parentFragment = (MarketsFragment) getParentFragment();
parentFragment.onCategorySelected(selectedCategory.getCategoryName(), selectedCategory.getCategoryParentName());
}
@Override
public void showResults(final Uri uri) {
LayoutUtils.showResults(getView(), headerView.getSpinnerId());
headerView.setVisibility(View.VISIBLE);
}
@Override
public void showNoResults(final Uri uri) {
final MarketsFragment parentFragment = (MarketsFragment) getParentFragment();
parentFragment.hideSpinner();
//LayoutUtils.showNoResult(getView(), headerView.getSpinnerId());
}
@Override
public void onDismiss(DialogInterface dialog) {
headerView.setSelected(false);
}
@Override
public void onNothingSelected(IcsAdapterView<?> parent) {
}
어떤 아이디어?
감사!
다음과 같이 할 수 있습니다.
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("yourSpinner", yourSpinner.getSelectedItemPosition());
// do this for each or your Spinner
// You might consider using Bundle.putStringArray() instead
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize all your visual fields
if (savedInstanceState != null) {
yourSpinner.setSelection(savedInstanceState.getInt("yourSpinner", 0));
// do this for each of your text views
}
}
도움이 되었기를 바랍니다
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다