首页 > 解决方案 > Spinner onItemSelected 未触发

问题描述

我试图为一个时间紧迫的大学项目制作一个应用程序(因此为混乱的代码道歉)这个特定活动的功能之一是当用户从微调器中选择一个 id 时,文档将为他们加载到屏幕上查看。但是,微调器无法识别 onItemSelected 方法并且没有运行任何内容,Log.d 也没有被点燃,因此该方法没有被启动。没有崩溃。

public class Manage extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    ...
    ...

    //The following class is initiating the on item selected viewer for the navigation bar to use to read inputs.
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                //If "Request" is clicked, opens the request activity.
                case R.id.navigation_Request:
                    Intent requestIntent = new Intent(Manage.this, Request.class);
                    Manage.this.startActivity(requestIntent);
                    return true;
                //If "Search" is clicked, opens the search activity.
                case R.id.navigation_Search:
                    Intent searchIntent = new Intent(Manage.this, Search.class);
                    Manage.this.startActivity(searchIntent);
                    return true;
                //if "Manage" is clicked, do nothing, as the manage activity is already open.
                case R.id.navigation_Manage:
                    return true;
                //If "Premium" is clicked, opens the Premium activity.
                case R.id.navigation_Premium:
                    Intent premiumIntent = new Intent(Manage.this, Premium.class);
                    Manage.this.startActivity(premiumIntent);
                    return true;
                //If "Quit" is clicked, opens an alert box to ask if the user is sure they wish to quit
                //If "Yes" is clicked, the app closes. If "No" is clicked, the alert box closes.
                case R.id.navigation_Quit:
                    new AlertDialog.Builder(Manage.this)
                            .setMessage("Are you sure you wish to quit?")
                            .setCancelable(false)
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    moveTaskToBack(true);
                                    finish();
                                }
                            })
                            .setNegativeButton("No", null)
                            .show();
            }
            return false;
        }
    };
    //When the activity is opened,the navigation bar is initiated, and its population is called.
    //The "Manage" part of the navigation bar is also set to be highlighted, as this is the users current activity.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setSelectedItemId(R.id.navigation_Manage);

        cloudstorage = FirebaseFirestore.getInstance();
        mAuth = FirebaseAuth.getInstance();
        spnTrades = findViewById(R.id.spnType);
        editDesc = findViewById(R.id.editDesc);
        editLocation = findViewById(R.id.editLocation);
        editPhone = findViewById(R.id.editPhone);
        quoteImageButton = findViewById(R.id.imgQuote2);
        quotelist = findViewById(R.id.spnQuoteList);

        populatespinnerarray();
        populatespinner();
    }


    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
        Log.d(TAG, "Selected item recognised");
        String selectedquote = quotelist.getItemAtPosition(position).toString();
        FirebaseUser user = mAuth.getCurrentUser();
        String uid = user.getUid();
            cloudstorage.collection("quotes")
                    .whereEqualTo("PhotoID", selectedquote)
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.isSuccessful()) {
                                for (QueryDocumentSnapshot document : task.getResult()) {
                                    if (document != null && document.exists()) {
                                        descfinal = document.getString("Description");
                                        typefinal = document.getString("Trade");
                                        phonefinal = document.getString("Phone");
                                        locationfinal = document.getString("Location");
                                        loadquoteinfo();
                                        }
                                    }
                                }
                            }
                        });
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                Log.d(TAG,"Nope.");

            }






    public void populatespinnerarray() {
        FirebaseUser user = mAuth.getCurrentUser();
        String uid = user.getUid();
        cloudstorage.collection("quotes")
                .whereEqualTo("UserID", uid )
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                if (document != null && document.exists()) {
                                    queryid = document.getString("PhotoID");
                                    quoteArray.add(queryid);
                                    Log.d(TAG,"Line" +queryid+"added");
                                }
                            }
                        } else {
                            Log.d(TAG, "Error populating spinner: ", task.getException());


                        }

                    }
                });

      }
    public void populatespinner() {
        adapter = new ArrayAdapter<String>(Manage.this,android.R.layout.simple_spinner_item, quoteArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        quotelist.setAdapter(adapter);


    }

    public void loadquoteinfo() {
        editDesc.setText(descfinal);
        editPhone.setText(phonefinal);
        editLocation.setText(locationfinal);

        if (typefinal == "Auto-Mechanic") {
            tradefinalpos = 1;
            spnTrades.setSelection(1);
        }
        else if (typefinal == "Roof Repair") {
            tradefinalpos = 2;
            spnTrades.setSelection(2);
        }
        else if (typefinal == "Carpentry") {
            tradefinalpos = 3;
            spnTrades.setSelection(3);
        }
        else if (typefinal == "Landscaping") {
            tradefinalpos = 4;
            spnTrades.setSelection(4);
        }
        else if (typefinal == "Decorating") {
            tradefinalpos = 5;
            spnTrades.setSelection(5);
        }
        else if (typefinal == "Plumbing") {
            tradefinalpos = 6;
            spnTrades.setSelection(6);
        }
        else if (typefinal == "Electrical") {
            tradefinalpos = 7;
            spnTrades.setSelection(7);
        }
        else if (typefinal == "Odd Jobs") {
            tradefinalpos = 8;
            spnTrades.setSelection(8);
        }





    }


}

标签: javaandroidgoogle-cloud-firestorespinner

解决方案


尝试在方法spinner.setOnItemSelectedListener(this);的末尾添加populatespinner(),其中“spinner”是您要设置侦听器的微调器。

这告诉微调器使用您创建的侦听器。


推荐阅读