Java Android:如何由此创建 Switch 案例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4227539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:47:37  来源:igfitidea点击:

Android: how to create Switch case from this?

javaandroid

提问by Riza

public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    AlertDialog.Builder adb = new AlertDialog.Builder(CategoriesTab.this);

    adb.setTitle("Selected Category");
    adb.setMessage("Selected Item is = "+lv1.getItemAtPosition(position));
    adb.setPositiveButton("Ok", null);
    adb.show();
}

This at the moment displays an alertbox when an item from listview is clicked. I want to convert the alertbox to load a specific xml for each choices clicked. How can i do this? thanks for your help.

当单击列表视图中的项目时,此时会显示一个警报框。我想将警报框转换为为单击的每个选项加载特定的 xml。我怎样才能做到这一点?谢谢你的帮助。

采纳答案by Ads

switch(position) {
    case 0:
        setContentView(R.layout.xml0);
        break;
    case 1:
        setContentView(R.layout.xml1);
        break;
    default:
        setContentView(R.layout.default);
        break;
}

i hope this will do the job!

我希望这能完成这项工作!

回答by Vladimir Ivanov

switch(position) {
  case 0:
    ...
    break;
  case 1:
    ...
    break;
  default:
    ...

}

Did you mean that?

你是那个意思吗?

回答by Litux

You can do this:

你可以这样做:

@Override
protected Dialog onCreateDialog(int id) {
    String messageDialog;
    String valueOK;
    String valueCancel;
    String titleDialog;
    switch (id) {

    case id:
        titleDialog = itemTitle;
        messageDialog = itemDescription
        valueOK = "OK";            
        return new AlertDialog.Builder(HomeView.this).setTitle(titleDialog).setPositiveButton(valueOK, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                Log.d(this.getClass().getName(), "AlertItem");
            }
        }).setMessage(messageDialog).create(); 

and then call to

然后调用

showDialog(numbreOfItem);

回答by Deepak Rawat

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.:

            break;
        case R.id.:

            break;
        default:
            break;
    }
}