Android Spinner在onItemSelected上不起作用

时间:2019-06-29 12:24:09

标签: android

我有一个微调器。我可以成功地将数据传输到微调器。但是微调器的onitemselected属性不起作用。我在哪里犯错?我希望微调器的onitemselected属性起作用。 onitemselected永远行不通。我创建了一个日志,但是此功能永远无法使用。

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnItemSelectedListener {
 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);

 public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        spinner.setSelection(position);

        if(parent.getId() == R.id.spinnerMain) {
        switch (position) {
            case 0:
                DatabaseReference dbProducts = FirebaseDatabase.getInstance().getReference("0").child("titles");

                dbProducts.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        productList = new ArrayList<>();

                        if(dataSnapshot.exists()){

                            for(DataSnapshot productSnapshot : dataSnapshot.getChildren()){
                                Product p = productSnapshot.getValue(Product.class);
                                productList.add(p);
                            }

                            adapter = new ProductsAdapter(MainActivity.this, productList);

                            recyclerView.setAdapter(adapter);

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
                Log.d("berkoooeeer", "asdasdasdasdasd");

            case 1:
                // Whatever you want to happen when the second item gets selected
                break;
            case 2:
                // Whatever you want to happen when the thrid item gets selected
                break;

        }
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub
    }

1 个答案:

答案 0 :(得分:0)

覆盖您的方法,因此您不会错过任何覆盖的方法参数。同时删除spinner.setSelection(position);行。您已经手动选择了它。

@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        if(parent.getId() == R.id.spinnerMain) {
       .........
       ..........
       ....
       ...
    }