简体   繁体   English

Android Firebase - 根据微调器更改来自 firebase 的列表视图数据

[英]Android Firebase - Change listview data from firebase based on spinner

Hi I'm trying to display certain data on my listview based on item selected from a spinner.您好我正在尝试根据从微调器中选择的项目在我的列表视图中显示某些数据。 This is how the display page currently looks.这是显示页面当前的样子。 It's reading all the data from the node.它正在从节点读取所有数据。 Because "Running" is selected on the spinner above, I want the list view to only show Running.因为在上面的微调器上选择了“Running”,所以我希望列表视图只显示 Running。

Here is the code i'm using to read all the data.这是我用来读取所有数据的代码。

 @Override protected void onStart() { super.onStart(); //reading data to listview, every time its saved currentUserDB2.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { cardiosList.clear(); for(DataSnapshot cardioHistorySnapshot: dataSnapshot.getChildren()){ Cardio cardio = cardioHistorySnapshot.getValue(Cardio.class); cardiosList.add(cardio); } CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList); ListViewCardioHistory.setAdapter(adapter); } @Override public void onCancelled(DatabaseError databaseError) { } }); }

Working code工作代码

 spinnerCardioHistory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { String name = spinnerCardioHistory.getSelectedItem().toString(); currentUserDB2.orderByChild("detailCategory").equalTo(name).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { cardiosList.clear(); for (DataSnapshot cardioHistorySnapshot: dataSnapshot.getChildren()){ Cardio cardio = cardioHistorySnapshot.getValue(Cardio.class); cardiosList.add(cardio); } CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList); ListViewCardioHistory.setAdapter(adapter); } @Override public void onCancelled(DatabaseError databaseError) { } }); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } });

If you want only the word Running to be displayed then do the following:如果您只想显示“正在Running ”一词,请执行以下操作:

 String text = spinner.getSelectedItem().toString();
 FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
 DatabaseReference currentUserDB2=FirebaseDatabase.getInstance().getReference().child("Cardio").child(user.getUid());
 currentUserDB2.orderByChild("detailCategory").equalTo(text).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            cardiosList.clear();
            for(DataSnapshot cardioHistorySnapshot : dataSnapshot.getChildren()){

               String category=cardioHistorySnapshot.child("detailCategory").getValue().toString();
                cardiosList.add(category);
            }

            CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList);
            ListViewCardioHistory.setAdapter(adapter);

First you get the select texted that is in the spinner String text = spinner.getSelectedItem().toString();首先,您获得微调器中的选择String text = spinner.getSelectedItem().toString();

then you add it in the query orderByChild("detailCategory").equalTo(text) and then retrieve from the database and it will only add the word that is selected in the spinner which is "Running".然后将其添加到查询orderByChild("detailCategory").equalTo(text)中,然后从数据库中检索,它只会添加在“正在运行”的微调器中选择的单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM