Android切换声明麻烦

时间:2011-02-14 17:40:53

标签: android listview switch-statement listactivity

我是Java和Android的新手,我想知道是否可以在案例中使用Intent来显示新背景,然后显示用户点击的项目的文本(例如;他们点击打开的联系人,我告诉他们打开的联系人是什么)还是有另一种方式?

public class ListActivityExample extends ListActivity{
static final String[] ACTIVITY_CHOICES = new String[] {
    "Open Website Example",
    "Open Contacts",
    "Open Phone Dialer Example",
    "Search 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;
            }
        }
    });
}
}

3 个答案:

答案 0 :(得分:0)

我注意到你错过了休息时间;案例2中的陈述意味着案件2将尝试执行案例2和案例3

答案 1 :(得分:0)

关于switch语句的两件事。

首先,你在案例2之后错过了休息时间。

其次,作为一个优秀的编程标准,你应该在实例化其中的新变量时用括号括起你的案例。

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

答案 2 :(得分:0)

我认为你需要定义一个ListView。还要在Layout中添加Listview。我已经分享了一个有效的参考代码:

    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