Android Studio中的“导航更改时Spinner项目丢失”
创始人
2024-08-18 13:30:15
0

在Android Studio中,当导航更改时,Spinner项目可能会丢失。以下是一个解决方法的代码示例:

  1. 创建一个全局变量来保存Spinner项目的选择位置:
private int spinnerSelection = 0;
  1. 在onCreate()方法中,将Spinner初始化为保存的选择位置:
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.spinner_items, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(spinnerSelection);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView parent, View view, int position, long id) {
        // 保存Spinner项目的选择位置
        spinnerSelection = position;
        
        // 执行Spinner项目选择后的操作
        // ...
    }

    @Override
    public void onNothingSelected(AdapterView parent) {
        // 未选择项目时的操作
    }
});
  1. 在Activity的onSaveInstanceState()方法中保存Spinner项目的选择位置:
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("spinnerSelection", spinnerSelection);
}
  1. 在Activity的onRestoreInstanceState()方法中恢复Spinner项目的选择位置:
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    spinnerSelection = savedInstanceState.getInt("spinnerSelection");
}

通过这些代码示例,当导航更改时,Spinner项目的选择位置将被保存和恢复,以确保项目不会丢失。

相关内容

热门资讯

必备攻略!werplan有挂,... 必备攻略!werplan有挂,红龙poker辅助平台(规律透视开挂辅助神器);必备攻略!werpla...
解密关于!购买的wpk辅助在哪... 您好:购买的wpk辅助在哪里下载这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
大家学习交流!wepoker辅... 大家学习交流!wepoker辅助是真的假的,wepoker俱乐部辅助器(新2026透视开挂辅助工具)...
推荐一款!wepoker私人局... 推荐一款!wepoker私人局外卦,pokemmo脚本最新版(规律透视开挂辅助软件)是一款可以让一直...
发现一款!werplan外卦神... 您好:德普辅助器可以用这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
总算清楚!aapoker安装包... 总算清楚!aapoker安装包可以使用,aapoker透视脚本(高科技透视开挂辅助工具);1、德州a...
新手必备!哈糖大菠萝能开挂,h... 新手必备!哈糖大菠萝能开挂,hhpoker可以控制牌(2026新版透视开挂辅助插件);哈糖大菠萝能开...
技术分享!wepoker辅助器... 技术分享!wepoker辅助器软件下载,pokerworld修改器(大神透视开挂辅助工具);技术分享...
玩家必看科普!哈糖大菠萝助手,... 您好,哈糖大菠萝助手这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这...
一分钟带你了解!wepoker... 一分钟带你了解!wepoker软件辅助程序,智星德州辅助译码插件靠谱(解说透视开挂辅助软件);1、完...