我有它不同的纱厂一个fragement。要让这些微调显示我正在使用自定义arrayAdapter(SpinnerHintAdapter)初始(非可选)值。这个类中唯一重要的事情是覆盖getCount将(),所以选择阵列的最后一个项目没有显示,这是初始值的存储位置。结果这一切工作正常,直到你旋转设备,那么微调设置就行了因为某些原因最后正常值,即使碎片类仍然认为它的设置初始值。结果任何人任何线索为什么发生这种情况和/或如何解决这个问题?
code样品:结果片段:
@覆盖公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ 查看rootView = inflater.inflate(R.layout.fragment_free_top_up,集装箱,FALSE); 微调宠=(微调)rootView.findViewById(R.id.pet); SpinnerHintAdapter<&CharSequence的GT; petAdapter = SpinnerHintAdapter.createFromResource(getActivity(), R.array.pet_array,android.R.layout.simple_spinner_item); petAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); pet.setAdapter(petAdapter); pet.setSelection(pet.getCount()); 返回rootView;}SpinnerHintAdapter:
@覆盖公众诠释的getCount(){ 诠释计数= super.getCount(); //最后一个项目将是心领神会。 返回计数> 0?计数 - 1:计数;}例如字符串数组
<字符串数组名=pet_array> <项目>是< /项目> <项目&GT否LT; /项目> <项目>(初值)LT; /项目>< /字符串数组>解决方案
还没有找到答案,这个具体问题,但用另一种方法来显示它可以在这里找到一个初始值:stackoverflow/a/12221309/3453217
任何澄清了什么毛病我第一次尝试仍然是受欢迎的。
I have a fragement with various Spinners in it. To have these spinners display an initial (non selectable) value I'm using a custom arrayAdapter (SpinnerHintAdapter). The only important thing this class does is override the getCount() so the last item of the selection-array isn't displayed, this is where the initial value is stored. This all works fine untill you rotate the device, then the spinners are set on the last normal value of the list for some reason, even though the Fragment class still thinks it's set on the initial value. Anyone any clue why this happens and / or how to solve this problem?
Code samples: fragment:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_free_top_up, container, false); Spinner pet = (Spinner) rootView.findViewById(R.id.pet); SpinnerHintAdapter<CharSequence> petAdapter = SpinnerHintAdapter.createFromResource(getActivity(), R.array.pet_array, android.R.layout.simple_spinner_item); petAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); pet.setAdapter(petAdapter); pet.setSelection(pet.getCount()); return rootView; }SpinnerHintAdapter:
@Override public int getCount() { int count = super.getCount(); // The last item will be the hint. return count > 0 ? count - 1 : count; }example string-array
<string-array name="pet_array"> <item>Yes</item> <item>No</item> <item>(initial value)</item> </string-array>解决方案
Haven't found an answer to this specific problem but used another method to display an initial value which can be found here: stackoverflow/a/12221309/3453217
Any clarification as to what went wrong with my first attempt is still welcome.
更多推荐
Android的微调作用于旋转怪异
发布评论