简体   繁体   English

在Android中调用一个意图变量

[英]Calling an Intent in Android that is a variable

I currently have a list view and I'm trying to direct the list view to different activities. 我目前有一个列表视图,并且试图将列表视图定向到其他活动。 So that if you click an item from let's say 1-4 you'll get the class that corresponds to that. 这样,如果您单击1-4中的某个项目,您将获得与之相对应的类。 The only way that I can think of doing it is grabbing the text of the item in the list view and starting the activity of that name. 我能想到的唯一方法是在列表视图中获取项目的文本并启动该名称的活动。 The code for it would go something like this: 它的代码将如下所示:

final String chosen = "";
chosen = (String) ((TextView) view).getText();
Intent nextScreen = new Intent(getApplicationContext(), chosen.class);

That does not work. 那行不通。 I get an error on the last line, saying that chosen cannot be resolved into a type. 我在最后一行收到一个错误,说不能将selected解析为一个类型。

I know that ((TextView) view).getText() works because 我知道((TextView) view).getText()之所以有效是因为

Log.d("Debug", "Test"+((TextView) view).getText());

gives me the correct chosen item in logcat. 给我在logcat中正确选择的项目。

Any ideas/suggestions? 有什么想法/建议吗? Thanks in advance 提前致谢

EDIT: 编辑:

I tried changing my code to this: 我尝试将代码更改为此:

String chosen = (String) ((TextView) view).getText();
try {
    Intent nextScreen = new Intent(getApplicationContext(), Class.forName(chosen));
    startActivity(nextScreen);
    Log.d("Debug", "Good"+((TextView) view).getText());
    } 
catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Log.d("Debug", "Bad"+((TextView) view).getText());
    }
    Log.d("Debug", "Final"+((TextView) view).getText());

Log cat gave me an output of 日志猫给了我一个输出

BadItem1
FinalItem1

I think I'm going about this the wrong way as someone pointed out. 正如有人指出的那样,我认为我正在以错误的方式进行操作。 I also think I should be using OnItemClickListener . 我也认为我应该使用OnItemClickListener I will try it and post my results for easier help in the future. 我将尝试一下并发布结果,以便日后获得更轻松的帮助。

The error is here: 错误在这里:

final String chosen = "";
chosen = (String) ((TextView) view).getText();

Since you declare chosen as final, you can assign it a value only once: 由于您声明selected为final,因此只能为其分配一个值:

final String chosen = (String) ((TextView) view).getText();

Moreover I suppose you want to start the Activity which has the name that is stored in variable chosen. 此外,我想您想启动活动,该活动的名称存储在所选变量中。 You cannot write chosen.class for this. 您不能为此编写selected.class。 The correct way to do this is: 正确的方法是:

Class.forName(chosen);

Hope this helps! 希望这可以帮助!

There is no Intent constructor that takes a Context and a String . 没有Intent构造函数需要ContextString You can probably do something like Class.forName(chosen) in the call to the Intent constructor. 您可以在对Intent构造函数的调用中执行Class.forName(chosen)之类的操作。

Calling .class on a variable gets the class for that variable, not the class for the content of the variable. 在变量上调用.class将获取该变量的类,而不是变量内容的类。

I think that you should use the OnItemClickListener on the ListView to identity the clicked item. 我认为您应该在ListView上使用OnItemClickListener来标识单击的项目。 The position parameter, or calling getItemAtPosition(position) , should be enough to identify uniquely a item in the listview and call the appropriate activity. position参数或调用getItemAtPosition(position)应该足以在列表视图中唯一标识一个项目并调用适当的活动。

See documentation here: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html 请参阅此处的文档: http : //developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

Make a Factory . 建立工厂

This way you don't have to tie your class names to the text the user is reading. 这样,您不必将类名与用户正在阅读的文本绑定在一起。 giving you more flexibility / better user experience / easier code to maintain. 为您提供更大的灵活性/更好的用户体验/更易于维护的代码。 Helping with your separation of concerns as well. 以及帮助您分离关注点

class NavFactory {
    private static final int CLASS_FIRST = 1;     

    public static Intent getNavIntent(Context context, String name){
         switch(getId(name)){
              case CLASS_FIRST:
                  return new Intent(context, FirstClass.class);
               default:
                  return null;
         }
    }

    private static int getId(String name){
       if("listItemOneText".equals(name)
            return CLASS_FIRST;

       return -1;
    }


}

Ref: Java Factory Pattern 参考: Java工厂模式

In your case: 在您的情况下:

String chosen = (String) ((TextView) view).getText();
Intent nextScreen = NavFactory.getNavIntent(this, chosen);

You are new sending String as the class of supposed Activity . 您是新发送String作为假定Activityclass This is what you need: 这是您需要的:

final String chosen = "";
chosen = ((TextView) view).getText();
Class<?> chosenActivity = Class.forName(chosen);
Intent nextScreen = new Intent(this, chosenActivity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM