如何在androidstudio中使用spinner获得recyclerview的结果

9bfwbjaz  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(423)

喂,你们能帮帮我吗?
我正在做一个应用程序,它将显示房屋。用户也可以过滤它使用微调器,以减轻他们的搜索。但是我很难过滤微调器,结果似乎无法显示在recyclerview中
主活动.java
公共类mainactivity扩展了appcompatactivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    EditText txtPremiseSearch = findViewById(R.id.txtPremiseSearch);
    RecyclerView rcvPremise = findViewById(R.id.rcvPremises);
    Spinner spnCategory = (Spinner) findViewById(R.id.spnCategory);

    //Spinner
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.premise_type));
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spnCategory.setAdapter(adapter);

    spnCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //txtPremiseSearch.setText(parent.getItemAtPosition(position).toString());
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    //Dummy list of premises
    Vector<Premises> premises = new Vector<>();
    Premises premises1 = new Premises(), premises2 = new Premises(), premises3 = new Premises();

    premises1.setPremiseID("001");
    premises1.setName("Medan Selera Presint 8");
    premises1.setRole("Restaurant");

    premises2.setPremiseID("002");
    premises2.setName("Old Precint Cafe");
    premises2.setRole("Cafe");

    premises3.setPremiseID("003");
    premises3.setName("Gloria Jean's Cafe");
    premises3.setRole("Cafe");

    premises.add(premises1);
    premises.add(premises2);
    premises.add(premises3);

    Toast.makeText(this, "Welcome " + intent.getStringExtra("name") + " to the Administration module!", Toast.LENGTH_LONG).show();

    rcvPremise.setAdapter(new PremisesAdapter(this, premises));
    rcvPremise.setLayoutManager(new LinearLayoutManager(this));

在此处输入图像描述

drkbr07n

drkbr07n1#

可能是因为回收器视图还没有更新。尝试在setadapter上运行setlayoutmanager b4,然后在用户单击微调器上的选定项时再次声明adapter和set adapter

相关问题