“setOnItemSelectedListener()” 在选择 Spinner 中的项目时未被调用。

huangapple go评论68阅读模式
英文:

"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&lt;DocumentSnapshot&gt;() {
           @Override
           public void onSuccess(DocumentSnapshot documentSnapshot) {
               if(documentSnapshot.exists()){
                   roomNumberDocument=documentSnapshot.getString(&quot;RoomNumber&quot;);
                   if(roomNumberDocument!=null){
                       for (int i = 100; i &lt; 111; i++) {
                           if(Integer.parseInt(roomNumberDocument)==i){
                               continue;
                           }
                           roomNumbersList.add(Integer.toString(i));
                       }
                   }else{
                       //room numbers
                       roomNumbersList.add(&quot;100&quot;);
                       roomNumbersList.add(&quot;101&quot;);
                       roomNumbersList.add(&quot;102&quot;);
                       roomNumbersList.add(&quot;103&quot;);
                       roomNumbersList.add(&quot;104&quot;);
                       roomNumbersList.add(&quot;105&quot;);
                       roomNumbersList.add(&quot;106&quot;);
                       roomNumbersList.add(&quot;107&quot;);
                       roomNumbersList.add(&quot;108&quot;);
                       roomNumbersList.add(&quot;109&quot;);
                       roomNumbersList.add(&quot;110&quot;);
                   }

               }
           }
       });

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&lt;&gt;(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&lt;?&gt; adapterView, View view, int i, long l) {
                strRoomNumber= adapterView.getItemAtPosition(i).toString();
                Toast.makeText(context, &quot;&quot;+strRoomNumber, Toast.LENGTH_LONG).show();
                Log.i(TAG, strRoomNumber);
            }

            @Override
            public void onNothingSelected(AdapterView&lt;?&gt; 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&lt;?&gt; 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, &quot;&quot;+strRoomNumber,  Toast.LENGTH_LONG).show();
            Log.i(TAG, strRoomNumber);

        }

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {

        }
    });

also check this out for more info
setOnItemSelectedListener of Spinner does not call

huangapple
  • 本文由 发表于 2020年9月23日 16:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64023739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定