微调器onItemSelected不触发

时间:2019-01-08 19:19:29

标签: java android google-cloud-firestore spinner

im试图为一个时间紧迫的大学项目开发​​一个应用程序(因此,为乱码表示歉意),此特定活动的功能之一是,当用户从微调器中选择一个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);
        }





    }


}

1 个答案:

答案 0 :(得分:0)

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

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