开发者

Android switch statement trouble

I'm new at Java and Android and I was wondering if it is possible to use an Intent in the case to bring up a new background followed by text for what item the user clicked (for example; they click open contacts and I tell them what open contacts is) or is there another way?

public class ListActivityExample extends ListActivity{
static final String[] ACTIVITY_CHOICES = new String[] {
    "Open Website Example",
    "Open Contacts",
    "Open Phone Dialer Example",
    "Searc开发者_JS百科h Google Example",
    "Start Voice Command"
};
final String searchTerms = "superman";

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, ACTIVITY_CHOICES));
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    getListView().setTextFilterEnabled(true);
    getListView().setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3){
            switch(arg2) {
            case 0: //opens web browser and navigates to given website
                 startActivity(new Intent(Intent.ACTION_VIEW,
                         Uri.parse("http://ww.android.com")));
                break;
            case 1: //opens phone dialer and fills in the given number
                {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("content://contacts/people/")));
                break;}
            case 2:            
                {
                  startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("tel:12125551212")));
                 break;}
            case 3: //
                {
                Intent intent= new Intent(Intent.ACTION_WEB_SEARCH);
                intent.putExtra(SearchManager.QUERY, searchTerms);
                startActivity(intent);
                break;}
            case 4: // 
                {startActivity(new
                                Intent(Intent.ACTION_VOICE_COMMAND));
                break;}
                default: break;
            }
        }
    });
}
}


I noticed you are missing a break; statement in case 2 which would mean that case 2 would try to execute case 2 and case 3


Two things about your switch statement.

First, you are missing the break after case 2.

Second, as a great programing standard you should enclose your cases with brackets when instantiating new variables within them.

case 1:
{
  int nice = 0;
  ...
} break;


I think you need to define a ListView. Also add a Listview in your Layout. I have shared a working reference code :

    public class MainMenu extends ListActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            try {
            String[] opt = getResources().getStringArray(R.array.MainMenu);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainmenu);

            ListView lv = getListView();
            ListAdapter la = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, opt);
            lv.setAdapter(la);
            lv.setTextFilterEnabled(true);
            lv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    switch (position) {
                    case 0:
                        Intent firstIntent = new Intent(MainMenu.this,
                                After1.class);
                        startActivity(firstIntent);
                        break;
                    case 1:
                        Intent secondIntent = new Intent(MainMenu.this,
                                After2.class);
                        startActivity(secondIntent);
                        break;

                    default:
                        break;
                    }

                }

                @SuppressWarnings("unused")
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }
            });

        } catch (Exception e) {
        }

    } // END onCreate()
}// END CLASS
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜