我在我的字符串文件中创建了一个数组(在values文件夹中)。 现在我想用它来选择旋转器,使用开关盒。 类似的东西:
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,workers);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
public void addUser(View view) {
switch (Arrays.toString(workers)){
case workers[0]: //this option isn't compiling
Waiter waiter = new Waiter();
waiter.setName(editName.getText().toString());
waiter.setLast(editLast.getText().toString());
waiter.setPass(editPass.getText().toString());
name = editName.getText().toString();
last = editLast.getText().toString();
passId = Integer.parseInt(editPass.getText().toString());
break;
case workers[1]:
break;
}
依旧......
编辑:
现在尝试使用if语句,这个方法应该在按下按钮时添加新工作者,但在那之后当我按下按钮时没有任何反应:
public void addUser(View view) {
if(Arrays.toString(workers).equals(workers[0])) {
Waiter waiter = new Waiter();
SQLiteDatabase usersDB = openOrCreateDatabase("USERES_DATABSE.sqlite", MODE_PRIVATE, null);
usersDB.execSQL("CREATE TABLE IF NOT EXISTS users_table (name TEXT, last TEXT, pass INTEGER)");
waiter.setName(editName.getText().toString());
waiter.setLast(editLast.getText().toString());
waiter.setPass(editPass.getText().toString());
name = editName.getText().toString();
last = editLast.getText().toString();
passId = Integer.parseInt(editPass.getText().toString());
if (editName.getText().toString().isEmpty() ||
editLast.getText().toString().isEmpty() ||
editPass.getText().toString().isEmpty()) {
AlertDialog alertDialog = new AlertDialog.Builder(UserCreatingActivity.this).create();
alertDialog.setTitle("Oops,");
alertDialog.setMessage("You forgot to fill something");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
} else {
usersDB.execSQL("INSERT INTO users_table VALUES('" + name + "','" + last + "','" + passId + "')");
AlertDialog alertSucsess = new AlertDialog.Builder(UserCreatingActivity.this).create();
alertSucsess.setTitle("Congrats,");
alertSucsess.setMessage(name + " " + last + " Has been created");
alertSucsess.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
alertSucsess.show();
}
usersDB.close();
// ADDING SHIFT MANAGER
} else if (Arrays.toString(workers).equals(workers[1])){
答案 0 :(得分:1)
您无法在交换机中使用变量。改为if。
或者你可以改为案例0:,案例1:,案例2:因为它总是工人[]
答案 1 :(得分:1)
case
表达式必须是常量值。
如果预定义了workers
的可能值,请将这些预定义和常量值用作case
表达式。
答案 2 :(得分:0)
正如大家提到的那样,无法使用带开关的阵列,因此您需要使用if 。你写条件的方式似乎不正确if(Arrays.toString(workers).equals(workers[0]))
。经过问题然后根据微调器中的选择你必须添加用户...如果我错了,请纠正我。所以首先你需要找到微调器的值
的解决方法1 强>
创建一个实例String变量并按如下方式实现onItemSelected。
String workerSelected;
//your code
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
workerSelected=worker[position]
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
或强>
由于您已添加spinner.setOnItemSelectedListener(this);
,因此只需直接覆盖方法,即代码足够
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
workerSelected=worker[position]
}
然后在 addUser方法中,你可以将if条件作为
if(workerSelected.equals(worker[0])){
//your code
}else if(workerSelected.equals(worker[1])){
//your code
}
<强>溶液2 强>
直接在 addUser方法
String workerSelected = spinner.getSelectedItem().toString();
if(workerSelected.equals(worker[0])){
//your code
}else if(workerSelected.equals(worker[1])){
//your code
}