肖像和风景savedInstanceState

时间:2014-02-06 19:48:04

标签: android spinner

我已经看过各种各样的事情来尝试解决这个问题,但是onCreate或者其他东西我都缺少了。

我可以在纵向和横向之间移动没问题,我甚至可以保存一些数据。我的问题出在某些纺纱厂。

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("NS1", ns1.getSelectedItemPosition());
    outState.putInt("NS2", ns2.getSelectedItemPosition());
    outState.putInt("NS3", ns3.getSelectedItemPosition());
    outState.putInt("NS4", ns4.getSelectedItemPosition());
    outState.putInt("NS5", ns5.getSelectedItemPosition());
    outState.putInt("NS6", ns6.getSelectedItemPosition());
    outState.putInt("AS", announcespinner.getSelectedItemPosition());
    outState.putInt("FS", feastselectorspinner.getSelectedItemPosition());
    outState.putInt("CS", choirspinner.getSelectedItemPosition());
    outState.putInt("SS", saintspinner.getSelectedItemPosition());
    outState.putBoolean("AK", akonly.isChecked());
    outState.putBoolean("PK", pkonly.isChecked());

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){

    feastselectorspinner.setSelection(savedInstanceState.getInt("FS"));
    pkonly.setChecked(savedInstanceState.getBoolean("PK"));
    akonly.setChecked(savedInstanceState.getBoolean("AK"));

            //savedInstanceWait = false;
            //updateNewSongSpinners();
    ns1.setSelection(savedInstanceState.getInt("NS1"));
    ns2.setSelection(savedInstanceState.getInt("NS2"));
    ns3.setSelection(savedInstanceState.getInt("NS3"));
    ns4.setSelection(savedInstanceState.getInt("NS4"));
    ns5.setSelection(savedInstanceState.getInt("NS5"));
    ns6.setSelection(savedInstanceState.getInt("NS6"));

    saintspinner.setSelection(savedInstanceState.getInt("SS"));
    choirspinner.setSelection(savedInstanceState.getInt("CS"));
    announcespinner.setSelection(savedInstanceState.getInt("AS"));

}

它会更新除nsx微调器之外的所有内容。我相信它有一些与这些可以改变的事实有关。

如果您选中/取消选中pkonly或akonly复选框或在feastselectorspinner中选择其他选项,则nsx微调器将根据下面执行的数据库查询进行调整

private void updateNewSongSpinners(){
    if(!savedInstanceWait){
        db = new DatabaseHandler(Announcer.this);
        List<NewSong> listnewsong = db.getAkOrPkOrFeastSongs(akonly.isChecked(), pkonly.isChecked(), feastselectorspinner.getSelectedItem().toString());
        List<String> list = new ArrayList<String>();
        String t;
        list.add("Select New Song");
        for(NewSong lns : listnewsong){
            t = "";
            if(lns.getSongNum() < 10){
                t = "00";
            }else if(lns.getSongNum() < 100){
                t = "0";
            }
            t += Integer.toString(lns.getSongNum()) + " " + lns.getSongTitle();
            list.add(t);
        }

        ArrayAdapter<String> adapter  = new ArrayAdapter<String>(Announcer.this, R.layout.list_item, list);
        adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);

        //reset all spinners
        ns1.setAdapter(adapter);
        ns2.setAdapter(adapter);
        ns3.setAdapter(adapter);
        ns4.setAdapter(adapter);
        ns5.setAdapter(adapter);
        ns6.setAdapter(adapter);
    }
}

我错过的是阻止nsx微调器返回其保存位置。检索到的NS1 ... NS6值是正确的数字。我无法调试这个,因为它不会在我的手机和我的模拟器上调试,当我在纵向和横向之间切换时,模拟器不做任何事情。

savedInstanceWait用于查看是否存在竞争条件。我找不到一个。

我不知所措。显然,在onRestoreInstanceState()之后调用feastselectorspinner onClick()。这导致纺纱厂明显重置。我不知道为什么feastselectorspinner会调用它的功能,而其他人都不会重置它们的字段,但无论如何。这是修复,我仍然想知道它为什么在恢复()之后调用onclick()

private void updateNewSongSpinners(){
    String str1, str2, str3, str4, str5, str6;

    str1 = ns1.getSelectedItem().toString();
    str2 = ns2.getSelectedItem().toString();
    str3 = ns3.getSelectedItem().toString();
    str4 = ns4.getSelectedItem().toString();
    str5 = ns5.getSelectedItem().toString();
    str6 = ns6.getSelectedItem().toString();


    db = new DatabaseHandler(Announcer.this);
    List<NewSong> listnewsong = db.getAkOrPkOrFeastSongs(akonly.isChecked(), pkonly.isChecked(), feastselectorspinner.getSelectedItem().toString());
    List<String> list = new ArrayList<String>();
    String t;
    list.add("Select New Song");
    for(NewSong lns : listnewsong){
        t = "";
        if(lns.getSongNum() < 10){
            t = "00";
        }else if(lns.getSongNum() < 100){
            t = "0";
        }
        t += Integer.toString(lns.getSongNum()) + " " + lns.getSongTitle();
        list.add(t);
    }

    ArrayAdapter<String> adapter  = new ArrayAdapter<String>(Announcer.this, R.layout.list_item, list);
    adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);

    //reset all spinners
    ns1.setAdapter(adapter);
    ns2.setAdapter(adapter);
    ns3.setAdapter(adapter);
    ns4.setAdapter(adapter);
    ns5.setAdapter(adapter);
    ns6.setAdapter(adapter);

    //if available keep previously chosen song
    for(int i = 0; i < list.size(); i++){
        if(list.get(i).equals(str1)) ns1.setSelection(i);
        if(list.get(i).equals(str2)) ns2.setSelection(i);
        if(list.get(i).equals(str3)) ns3.setSelection(i);
        if(list.get(i).equals(str4)) ns4.setSelection(i);
        if(list.get(i).equals(str5)) ns5.setSelection(i);
        if(list.get(i).equals(str6)) ns6.setSelection(i);
    }
}

0 个答案:

没有答案