编辑:Rooms从数据库中绘制其ArrayList。当我使用静态ArrayList时,我没有这个问题,但是在ArrayList中却没有这个问题,我是从数据库中提取的。我正在拉元素,但是单击时似乎为空。
我有一个微调器。我已将项目添加到此微调器。但是AdapterView.Onitemselectedlistener进程不能执行,该功能不起作用。我选择的数据不可读。如何读取微调器中选择的数据?此版本的代码不起作用,所选数据不会出现。问题出在哪里?
XML源:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#28B2C0"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<Spinner
android:id="@+id/spinnerMain"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"/>
</android.support.v7.widget.Toolbar>
源代码:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemSelectedListener, View.OnTouchListener {
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = mDatabase.child("0").child("Rooms");
private List<String> Rooms=new ArrayList<String>(10);
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spinner = (Spinner)findViewById(R.id.spinnerMain);
ArrayAdapter<String>adapterSpinner = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,Rooms);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setOnItemSelectedListener(MainActivity.this);
adapterSpinner.notifyDataSetChanged();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String data = snapshot.getValue(String.class);
Rooms.add(data);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "AWW SNAP... Something is Wrong.", Toast.LENGTH_LONG).show();
}
});
final AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
Toast.makeText(MainActivity.this, "asdasdadsa", Toast.LENGTH_SHORT).show();
// break;
case 1:
Toast.makeText(MainActivity.this, "asdasdadsa", Toast.LENGTH_SHORT).show();
// break;
case 2:
// Whatever you want to happen when the thrid item gets selected
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
};
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "Selected", Toast.LENGTH_SHORT).show();
spinner.setOnItemSelectedListener(listener);
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
答案 0 :(得分:0)
试试这个:
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (Rooms.get(i).equals("Room 1"){
Toast.makeText(MainActivity.this, "asdasdadsa", Toast.LENGTH_SHORT).show();
// break;
} else if (Rooms.get(i).equals("Room 2") {
Toast.makeText(MainActivity.this, "asdasdadsa", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
您的代码有点混乱。您在spinner.setOnItemSelectedListener(listener);
中使用了spinner.setOnItemSelectedListener(new ...)
,并且spinner.setOnItemSelectedListener(MainActivity.this);
也被用来设置监听器。
当我使用您的代码时,一旦打开MainActivity,就会显示“选定”吐司。然后,您的listener
变量代替了Spinner的OnItemSelectedListener。
也许您的问题是您忘记了setContentView
?
因此,请尝试以下代码:
public class MainActivity extends AppCompatActivity {
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinnerMain);
List<String> Rooms = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Rooms.add("room" + i);
}
ArrayAdapter<String> adapterSpinner = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, Rooms);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
final AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
Toast.makeText(MainActivity.this, "select 0", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this, "select 1", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(MainActivity.this, "select others", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
};
spinner.setOnItemSelectedListener(listener);
}
}
或:
public class MainActivity extends AppCompatActivity {
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinnerMain);
List<String> Rooms = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Rooms.add("room" + i);
}
ArrayAdapter<String> adapterSpinner = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, Rooms);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
Toast.makeText(MainActivity.this, "select 0", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this, "select 1", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(MainActivity.this, "select others", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
或:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinnerMain);
List<String> Rooms = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Rooms.add("room" + i);
}
ArrayAdapter<String> adapterSpinner = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, Rooms);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
Toast.makeText(MainActivity.this, "select 0", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this, "select 1", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(MainActivity.this, "select others", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}