Android:选项菜单清除我的Spinners,如何更新它们?

时间:2012-01-31 00:22:43

标签: android refresh spinner options-menu

我的Android应用有两个Spinners(下拉菜单)正常工作。

但是,现在我添加了一个OptionsMenu:

  1. 一个子菜单,包含打开AlertDialog文本框的选项。
  2. 一个子菜单,可选择更改语言(区域设置)。
  3. 如果我在第一个子菜单中单击一个选项,我的两个微调器中的内容都将被删除,然后文本框将被打开。当我从文本框中点击自己时,旋转器仍然被清除。

    同样,如果我点击自己更改语言,两个微调器都会被清除,而程序的其余部分会更新为新语言。通过重新启动应用程序,所有内容都会恢复。

    如果重要,旋转器将从ArrayAdapter获取其内容。

    所以我想知道,如何更新旋转器?我已尝试使用onCreate,onResume或onConfigurationChanged中的代码“重新填充”微调器,但它没有做任何更改。

    如果需要,我可以找到更多代码,但至少在这里有一些代码:

    Spinner spinner1;
    Spinner spinner2;
    ArrayAdapter<CharSequence> adapter1;
    ArrayAdapter<CharSequence> adapter2;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
    
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        adapter1 = ArrayAdapter.createFromResource(
                this, R.array.some_array, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        spinner1.setSelection(1);
    
        // Same procedure for spinner2...
        }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Resources res = getResources();
        SubMenu faqMenu = menu.addSubMenu(0, 100, 1, res.getString(R.string.FAQ)).setIcon(android.R.drawable.ic_menu_rotate);
            faqMenu.add(1, 101, 0, R.string.someText);
        SubMenu langMenu = menu.addSubMenu(0, 200, 2, res.getString(R.string.language)).setIcon(android.R.drawable.ic_menu_rotate);
            langMenu.add(1, 201, 0, "Language1");
            langMenu.add(1, 202, 0, "Language2");
            langMenu.add(1, 203, 0, "Language3");
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
    
        case 101:
            AlertDialog.Builder textbox = new AlertDialog.Builder(main.this);
            Resources res = getResources();
            textbox.setMessage(res.getString(R.string.someText));
            textbox.show();
            this.setContentView(R.layout.main);
            try{
                spinner1.setAdapter(adapter1);
                spinner2.setAdapter(adapter2);
                spinner1.setSelection(1);
                spinner2.setSelection(2);
            } catch (Exception e){
                e.printStackTrace();
            }
            break;
    
        case 201:
    
            Locale locale = new Locale("en"); 
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            Toast.makeText(this, "Here is text in Language1! But unfortunately you need to restart the app before the spinners start working again...sorry!", Toast.LENGTH_LONG).show();
            this.setContentView(R.layout.main);
            try{
                spinner1.setAdapter(adapter1);
                spinner2.setAdapter(adapter2);
                spinner1.setSelection(1);
                spinner2.setSelection(2);
            } catch (Exception e){
                e.printStackTrace();
            }
            break;
    
        // Similar code for options 202, 203
    
        }
        return super.onOptionsItemSelected(item);
    }
    
    final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            // Perform action on click
            public void onClick(View v) {
                // Make text box
                AlertDialog.Builder textbox = new AlertDialog.Builder(MyActivity.this);
                Resources res = getResources();
    
                // Get the chosen values from the spinners
                String spinner1choice = spinner1.getSelectedItem().toString();
                String spinner2choice = spinner2.getSelectedItem().toString();
    
                // Some code for showing the selected items.
         }
    }
    

    所以......任何人都对如何更新/刷新微调器有任何帮助吗?

1 个答案:

答案 0 :(得分:0)

删除this.setContentView(R.layout.main);

之前

 try{
            spinner1.setAdapter(adapter1);
            spinner2.setAdapter(adapter2);
            spinner1.setSelection(1);
            spinner2.setSelection(2);
        } catch (Exception e){
            e.printStackTrace();
        }

在你的两个案件中。

下面的工作代码:

public class TestSpinner extends Activity {

private ArrayAdapter<CharSequence> adapter1;
private Spinner spinner1;
private ArrayAdapter<CharSequence> adapter2;
private Spinner spinner2;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.spinner);
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    adapter1 = ArrayAdapter.createFromResource(
            this, R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    MyOnItemSelectedListener listener = new MyOnItemSelectedListener();

    spinner1.setOnItemSelectedListener(listener);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    adapter2 = ArrayAdapter.createFromResource(
            this, R.array.planets_array1, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter1);
    spinner2.setOnItemSelectedListener(listener);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    SubMenu faqMenu = menu.addSubMenu(0, 100, 1, "FAQ").setIcon(android.R.drawable.ic_menu_rotate);
    faqMenu.add(1, 101, 0, "set other planents");
    SubMenu langMenu = menu.addSubMenu(0, 200, 2, "LANGAUGE").setIcon(android.R.drawable.ic_menu_rotate);
    langMenu.add(1, 201, 0, "Language1");
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case 101:
            AlertDialog.Builder textbox = new AlertDialog.Builder(TestSpinner.this);
            textbox.setMessage("You've set new planets");
            textbox.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dlg, int i) {
                    dlg.dismiss();
                }
            });
            textbox.show();
            try {
                spinner1.setAdapter(adapter2);
                spinner2.setAdapter(adapter2);
                spinner1.setSelection(1);
                spinner2.setSelection(2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        case 201:
            Locale locale = new Locale("en");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            Toast.makeText(this, "you've set old planets back!", Toast.LENGTH_LONG).show();
            try {
                spinner1.setAdapter(adapter1);
                spinner2.setAdapter(adapter1);
                spinner1.setSelection(1);
                spinner2.setSelection(2);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return true;
    }
    return false;
}

public class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent,
                               View view, int pos, long id) {
        Toast.makeText(parent.getContext(), "The planet is " +
                parent.getItemAtPosition(pos).toString(), 1).show();
    }

    public void onNothingSelected(AdapterView parent) {
    }
}

private void refresh() {
        finish();
        Intent myIntent = new Intent(TestSpinner.this, TestSpinner.class);
        startActivity(myIntent);
    }
}

干杯