问题描述
我使用的是微调下拉我的code,其中我有4〜5动态填充值,说如果我有苹果设置为默认,我从下拉橘子和旋转我的屏幕从纵向横向,它可以追溯到默认的苹果与it.How相关做我保存状态,使得沿看法时,我选择橘子,并旋转为横向,它填充所选值/保持在相同选择状态,并保持该视图完好/填充被在对应于所选值的人像模式选择的图。这里的适配器code我使用的是相同的:
I am using a spinner dropdown in my code , in which I have 4 to 5 dynamically populated values, say if I have "apples" set to default and I select "oranges" from the dropdown and rotate my screen to landscape from portrait, it goes back to default "apples" along with the view associated with it.How do I save the state such that when I select "oranges" and rotate to landscape, it populates the selected value/remains in the same selected state and keeps the view intact/populates the view that was selected in the portrait mode corresponding to the selected value. Here's the adapter code I use for the same:
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);
}
}
在各自的片段相应的使用:
Corresponding usage in the respective fragment:
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) {
}
任何想法?
谢谢!
推荐答案
您可以做到这一点像...
You can do this like...
@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
}
}
希望这有助于
这篇关于我如何preserve的方向改变而选择微调/下拉列表项的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!