【问题标题】:Having an issue with switch statement involving Android Spinners涉及 Android Spinner 的 switch 语句存在问题
【发布时间】:2012-06-17 04:28:22
【问题描述】:

我正在尝试在我的视图上使用 2 个微调器,目前正在实现“OnItemSelected”方法。我设置了一个 switch 语句来区分微调器,但由于某种原因它似乎不起作用。

这是我的代码:

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;
    import java.util.List;
    import java.util.ArrayList;
    import android.widget.AdapterView.OnItemSelectedListener;
    /**
     * This is the activity for feature 1 in the dashboard application.
     * It displays some text and provides a way to get back to the home activity.
     *
     */

    public class F1Activity extends DashboardActivity implements OnItemSelectedListener
    {

    /**
     * onCreate
     *
     * Called when the activity is first created. 
     * This is where you should do all of your normal static set up: create views, bind data to lists, etc. 
     * This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
     * 
     * Always followed by onStart().
     *
     * @param savedInstanceState Bundle
     */





    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView (R.layout.activity_f1);
        setTitleFromActivityLabel (R.id.title_text);

        //declaring variables

        Button submitButton = (Button) findViewById(R.id.button1);
        Button cancelButton = (Button) findViewById(R.id.button2);

        //getting arrays from strings file
        String[] regions = getResources().getStringArray(R.array.regions_array);
        String[] grids = getResources().getStringArray(R.array.grids_array);

        Spinner gridSpinner = (Spinner) findViewById(R.id.gridSpinner);
        Spinner regionSpinner = (Spinner) findViewById(R.id.regionSpinner);

        //creating adapters for both spinners

        ArrayAdapter<String> dataAdapter = 
            new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, grids);
        ArrayAdapter<String> regionAdapter = 
                new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, regions);

        // drop down layout style with radio button type.

        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapters to spinners
        gridSpinner.setAdapter(dataAdapter);
        regionSpinner.setAdapter(regionAdapter);
        gridSpinner.setOnItemSelectedListener(this);
        regionSpinner.setOnItemSelectedListener(this);

        submitButton.setOnClickListener(new View.OnClickListener() {
               public void onClick(View view){
                   Intent changeAdd = new Intent();
                   setResult(RESULT_OK, changeAdd);

                   EditText nameText = (EditText) findViewById(R.id.nameTextBox);
                   EditText passText = (EditText) findViewById(R.id.passwordTextBox);

                   if(nameText.getText().toString().length() > 0 && 
                           passText.getText().toString().length() > 0) //TAKE CARE OF LISRVIEW/DROPDOWN
                   {
                         Toast.makeText(getApplicationContext(), 
                           "Loading...", Toast.LENGTH_LONG).show();
                            // make an intent to start the virtual world activity..................like in addGridActivity/screen switch!
                            finish();

                   }
                   else
                   {
                       Toast.makeText(getApplicationContext(), "Sorry, you have to complete all the fields", 
                               Toast.LENGTH_SHORT).show();

                   }


               }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
               public void onClick(View view){
                   Intent changeAdd = new Intent();
                   setResult(RESULT_OK, changeAdd);
                   // cancelled and went back to home screen

                   finish();


               }
        });

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long arg3) {
        // to handle the selection of the gridSpinner or the regionSpinner
        int id = parent.getId();

        switch(id)
        {
        case R.id.gridSpinner:
            Toast.makeText(parent.getContext(), "you selected" +
            parent.getItemAtPosition(pos).toString() + "from the grid spinner", Toast.LENGTH_LONG);
            break;
        case R.id.regionSpinner:
            Toast.makeText(parent.getContext(), "you selected" + 
            parent.getItemAtPosition(pos).toString() + "from the region spinner", Toast.LENGTH_LONG);
            break;
        default:
            break;
        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }



} // end class


<code>

【问题讨论】:

  • 您是否尝试过view.getId()
  • 我看不出有什么问题。添加Log.v 或使用调试器查看getId() 返回的内容,并根据您的R id 检查它对应的内容。
  • 感谢您的建议。我只是在切换之前尝试了“int i = view.getId()”,但没有任何区别。

标签: android spinner switch-statement


【解决方案1】:

您的开关工作正常。它似乎不起作用的原因是因为您显示 Toast 的代码缺少 show() 调用。

代替:

        Toast.makeText(parent.getContext(), ..., Toast.LENGTH_LONG);

这样做:

        Toast.makeText(parent.getContext(), ..., Toast.LENGTH_LONG).show();

我自己也经常犯同样的错误:)

【讨论】:

    【解决方案2】:

    int id = parent.getId(); 替换为int id = view.getId();

    【讨论】:

    • 是的,这似乎没什么区别。当我这样做时,不会弹出任何消息。我确实尝试调试并查看 id 的变量,但只得到了一些长数字,例如 2131165197。我不知道它是否匹配。在我的 xml 文件中,实际的微调器被命名为 gridSpinner 和 regionSpinner。
    • @user1461393 该数字是视图的 ID。这些数字在编译时生成并驻留在 R.java 文件中,因此您可以在 R 文件中找到该数字并查看匹配的字符串 ID。
    【解决方案3】:

    您需要根据视图的 id 而不是视图适配器进行切换。尝试将int id = parent.getId(); 更改为int id = view.getId();

    【讨论】:

    • 我已经尝试过了,但由于某种原因它不起作用。我很感激你尝试。到目前为止,当我选择我的微调器时,什么都没有发生.....
    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多