英文:
"setOnItemSelectedListener()" not called when selecting item in spinner
问题
我正在创建一个用于酒店管理的安卓应用程序。
我想向用户展示可用房间数量,以下拉列表的形式。
我正在使用 Google Firebase 存储详细信息(房间号码、姓名、地址等)。
如果用户首次登录应用程序,下拉列表中应显示所有房间号码。但是,如果用户已经预订了房间,则该房间号码不应在下拉列表中可见。
以下是 Java 代码部分:
documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
roomNumberDocument = documentSnapshot.getString("RoomNumber");
if (roomNumberDocument != null) {
for (int i = 100; i < 111; i++) {
if (Integer.parseInt(roomNumberDocument) == i) {
continue;
}
roomNumbersList.add(Integer.toString(i));
}
} else {
// 房间号码
roomNumbersList.add("100");
roomNumbersList.add("101");
roomNumbersList.add("102");
roomNumbersList.add("103");
roomNumbersList.add("104");
roomNumbersList.add("105");
roomNumbersList.add("106");
roomNumbersList.add("107");
roomNumbersList.add("108");
roomNumbersList.add("109");
roomNumbersList.add("110");
}
}
}
});
如果房间已经预订,roomNumberDocument
将不为 null,否则将为 null。
如果房间已经预订,则不会将其添加到 ArrayList roomNumbersList
中。
以下是设置适配器的部分:
adapterRoomNumbers = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, roomNumbersList);
adapterRoomNumbers.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerRoomNumber.setAdapter(adapterRoomNumbers);
以下是下拉列表的点击监听器:
spinnerRoomNumber.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
strRoomNumber = adapterView.getItemAtPosition(i).toString();
Toast.makeText(context, "" + strRoomNumber, Toast.LENGTH_LONG).show();
Log.i(TAG, strRoomNumber);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
问题在于如果我在下拉列表中选择任何项,setOnItemSelectedListener()
不会被调用。
由于这个问题,strRoomNumber
得到了 null 值。我会使用 strRoomNumber
将其存储在 Firebase 数据库中。
完整的代码链接。
可能的问题是什么?
英文:
I'm creating an android app which is used for hotel management.
I want to show the user, the number of rooms available in the form of spinner.
I'm storing the details(roomNumber, name address etc..) in google firebase.
If the user is logging in the app for first time then all the room numbers in spinner should be visible. But if the user has already booked a room then that room number should not be visible in spinner.
Here is the java code:
documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.exists()){
roomNumberDocument=documentSnapshot.getString("RoomNumber");
if(roomNumberDocument!=null){
for (int i = 100; i < 111; i++) {
if(Integer.parseInt(roomNumberDocument)==i){
continue;
}
roomNumbersList.add(Integer.toString(i));
}
}else{
//room numbers
roomNumbersList.add("100");
roomNumbersList.add("101");
roomNumbersList.add("102");
roomNumbersList.add("103");
roomNumbersList.add("104");
roomNumbersList.add("105");
roomNumbersList.add("106");
roomNumbersList.add("107");
roomNumbersList.add("108");
roomNumbersList.add("109");
roomNumbersList.add("110");
}
}
}
});
If the room is already booked then roomNumberDocument
will not be null else it will be null.
If the room is already booked then I will not add it into the ArrayList roomNumbersList
.
Here I'm setting up the adapter:
adapterRoomNumbers= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, roomNumbersList);
adapterRoomNumbers.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerRoomNumber.setAdapter(adapterRoomNumbers);
Here is the click listener for the spinner:
spinnerRoomNumber.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
strRoomNumber= adapterView.getItemAtPosition(i).toString();
Toast.makeText(context, ""+strRoomNumber, Toast.LENGTH_LONG).show();
Log.i(TAG, strRoomNumber);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
The problem is that setOnItemSelectedListener()
is not called if I select any item in the spinner.
Because of that strRoomNumber
is getting null. I will use strRoomNumber
for storing it in the firebase database.
Here is my full code
What might be the problem?
答案1
得分: 1
尝试将设置适配器的代码移到documentReference.get().addOnSuccessListener
之前,并在onSuccess方法内的最后一行添加此代码:adapterRoomNumbers.notifyDataSetChanged();
解释:由于对 Firebase 的调用是异步过程,适配器未能正确更新。
英文:
Try move the codes for setting up the adapter to just before documentReference.get().addOnSuccessListener .......
and inside onSuccess method, add this as the last line: adapterRoomNumbers.notifyDataSetChanged();
Explanation: Adapter is not updated correctly as calls to firebase is an async process.
答案2
得分: 0
你的 setOnItemSelectedListner
必须像这样:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int i, long id) {
strRoomNumber = adapterView.getItemAtPosition(i).toString;
if (item != null) {
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, "" + strRoomNumber, Toast.LENGTH_LONG).show();
Log.i(TAG, strRoomNumber);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
另外,查看这个链接以获取更多信息:Spinner 的 setOnItemSelectedListener 未被调用
英文:
your setOnItemSelectedListner
must look like this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int i, long id) {
strRoomNumber = adapterView.getItemAtPosition(i).toString;
if (item != null) {
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, ""+strRoomNumber, Toast.LENGTH_LONG).show();
Log.i(TAG, strRoomNumber);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
also check this out for more info
setOnItemSelectedListener of Spinner does not call
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论