我想评估我在java类中的位置,并使用if语句运行一些代码。
这是我在Android Studio中的代码的一部分
public class UsefulGUI extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button();
button.setText("Enter your info");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dialogBox();
}
});
StackPane pane = new StackPane();
pane.getChildren().add(button);
Scene scene = new Scene(pane, 500, 300);
primaryStage.setTitle("Useful GUI");
primaryStage.setScene(scene);
primaryStage.show();
}
public class User {
private String name;
private String email;
private String phone;
public User(String n, String e, String p) {
this.name = n;
this.email = e;
this.phone = p;
}
@Override
public String toString() {
return "Name: " + name + " Email: " + email + " Phone: " + phone;
}
}
public void dialogBox() {
Dialog<User> dialog = new Dialog<>();
ArrayList<User>list = new ArrayList<User>();
dialog.setTitle("User info");
Label name = new Label("Name: ");
Label email = new Label("Email: ");
Label phone = new Label("Phone: ");
TextField textName = new TextField();
TextField textEmail = new TextField();
TextField textPhone = new TextField();
GridPane grid = new GridPane();
grid.add(name, 1, 1);
grid.add(textName, 2, 1);
grid.add(email, 1, 2);
grid.add(textEmail, 2, 2);
grid.add(phone, 1, 3);
grid.add(textPhone, 2, 3);
dialog.getDialogPane().setContent(grid);
ButtonType buttonType = new ButtonType("Ok", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(buttonType, ButtonType.CANCEL);
dialog.setResultConverter(new Callback<ButtonType, User>() {
@Override
public User call(ButtonType b) {
if (b == buttonType) {
return new User(textName.getText(), textEmail.getText(), textPhone.getText());
}
return null;
}
});
Optional<User>info = dialog.showAndWait();
// Adds user to list
list.add(new User(textName.getText(), textEmail.getText(), textPhone.getText()));
System.out.print("Info Added.\n NEWUSER ");
System.out.println(new User(textName.getText(), textEmail.getText(), textPhone.getText()));
//Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(i + ". " + list.get(i));
}
//if (info.isPresent()) {
// System.out.println(info.get());
//}
}
public void quitApplication() {
Platform.exit();
}
public static void main(String[] args) {
launch(args);
}
}
`
正如你所看到我放置if(PizzaActivity .class!= null){}但是我知道这是错误的。
它是一个Android项目。
我希望你能帮助我,提前谢谢你。
问候。
答案 0 :(得分:0)
像这样更改onBindViewHolder
方法:
.....
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// no neeed this condition
//if (PizzaActivity.class != null){
if(position == 0){
Intent intent = new Intent(activity, Pizza1Activity.class);
activity.startActivity(intent);
}
if(position == 1){
Intent intent = new Intent(activity, Pizza2Activity.class);
activity.startActivity(intent);
}
if(position == 2){
Intent intent = new Intent(activity, Pizza3Activity.class);
activity.startActivity(intent);
}
}
});
希望它能解决你的问题。
答案 1 :(得分:0)
尝试将位置设置为holder.itemView
中的标记
并在onClick()
方法中,使用view.getTag()
检索相同的位置并采取相应的行动。
答案 2 :(得分:-1)
班级不要检查空。如果要检查存在类,可以参考:
public boolean isClass(String className) {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
和
if (isClass("android.app.ActionBar")) {
Toast.makeText(getApplicationContext(), "YES", Toast.LENGTH_SHORT).show();
}
我希望它可以帮助你解决问题!