根据按钮选择从 Firebase 过滤所需数据

Filtering the required data from Firebase based on the button selection

我有两个活动,一个 (MainActivity) 可以选择 select 您想要查看的事件类别,第二个 activity(EventView) 向您显示那些特定的基于您的 selection 的事件。类别选项是: 拱 艺术 金融 科学

在下一个 activity 中,我希望来自 Firebase 的数据以基于类别 I select 的回收器视图的形式出现,但是正在调用我来自 Firebase 的所有数据。我使用 orderByChild 在我的 databaseReference 旁边添加了一个查询,以获取我需要的数据。

MainActivity.java

package com.example.admin.trialforevents;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button b1, b2, b3, b4;

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

        b1 = (Button) findViewById(R.id.archBttn);
        b2 = (Button) findViewById(R.id.artBttn);
        b3 = (Button) findViewById(R.id.scienceBttn);
        b4 = (Button) findViewById(R.id.financeBttn);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
    }

    public void onClick(View view){
        Toast.makeText(this, "Button Selected !", Toast.LENGTH_SHORT).show();
        Intent i = new Intent(getApplicationContext(), EventView.class);


    switch(view.getId()){
        case R.id.archBttn:
            String arch = "Architecture";
            i.putExtra("Category", arch);
            break;

        case R.id.artBttn:
            String art = "Art";
            i.putExtra("Category", art);
            break;

        case R.id.scienceBttn:
            String science = "Science";
            i.putExtra("Category", science);
            break;

        case R.id.financeBttn:
            String fin = "Finance";
            i.putExtra("Category", fin);
            break;
    }
        startActivity(i);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.admin.trialforevents.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/archBttn"
        android:text="Arch"
        android:layout_marginTop="41dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="Art "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/archBttn"
        android:layout_alignRight="@+id/archBttn"
        android:layout_alignEnd="@+id/archBttn"
        android:layout_marginTop="33dp"
        android:id="@+id/artBttn" />

    <Button
        android:text="Finance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/financeBttn"
        android:layout_below="@+id/artBttn"
        android:layout_alignLeft="@+id/artBttn"
        android:layout_alignStart="@+id/artBttn"
        android:layout_marginTop="32dp" />

    <Button
        android:text="Science"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/financeBttn"
        android:layout_alignRight="@+id/financeBttn"
        android:layout_alignEnd="@+id/financeBttn"
        android:layout_marginTop="33dp"
        android:id="@+id/scienceBttn" />
</RelativeLayout>

EventView.java

package com.example.admin.trialforevents;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

public class EventView extends AppCompatActivity{

    DatabaseReference databaseReference;

    RecyclerView recyclerView;


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

        databaseReference = FirebaseDatabase.getInstance().getReference().child("ApprovedEvents");


        recyclerView = (RecyclerView) findViewById(R.id.request_EventList);

        //Avoid unnecessary layout passes by setting setHasFixedSize to true
        recyclerView.setHasFixedSize(true);

        //Select the type of layout manager you would use for your recyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    protected void onStart() {
        final String event_cat  = getIntent().getStringExtra("Category");
        super.onStart();
        FirebaseRecyclerAdapter<Event, RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, RequestViewHolder>(
                Event.class,
                R.layout.event_list_row,
                RequestViewHolder.class,
                databaseReference.orderByChild(event_cat)
        ) {
            @Override
            protected void populateViewHolder(RequestViewHolder viewHolder, Event model, int position) {
                    viewHolder.setTitle(model.getTitle());
                    viewHolder.setDesc(model.getDesc());
                    viewHolder.setCategory(model.getCategory());
                    viewHolder.setLocation(model.getLocation());
                    viewHolder.setPrice(model.getPrice());
                    viewHolder.setImageUrl(getApplicationContext(), model.getImageUrl());
                viewHolder.imageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(EventView.this, "Image Selected", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        recyclerView.setAdapter(firebaseRecyclerAdapter);
    }

    public static class RequestViewHolder extends RecyclerView.ViewHolder {
        View mView;
        ImageView imageButton;


        public RequestViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            imageButton = (ImageView) mView.findViewById(R.id.request_image);
        }

        public void setTitle(String title) {
            TextView a_title = (TextView) mView.findViewById(R.id.request_title);
            a_title.setText(title);
        }

        public void setDesc(String desc) {
            TextView a_desc = (TextView) mView.findViewById(R.id.request_desc);
            a_desc.setText(desc);
        }

        public void setLocation(String location) {
            TextView a_desc = (TextView) mView.findViewById(R.id.request_location);
            a_desc.setText(location);
        }


        public void setCategory(String category) {
            TextView a_category = (TextView) mView.findViewById(R.id.request_category);
                a_category.setText(category);
        }

        public void setPrice(String price) {
            TextView a_price = (TextView) mView.findViewById(R.id.request_price);
            a_price.setText(price);
        }

        public void setImageUrl(Context ctx, String imageUrl) {
            ImageView a_image = (ImageView) mView.findViewById(R.id.request_image);
            Picasso.with(ctx).load(imageUrl).into(a_image);
        }
    }
}

activity_event_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.admin.trialforevents.EventView">
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/request_EventList"
        android:clickable="true"/>

</RelativeLayout>

Event.java

package com.example.admin.trialforevents;

public class Event {

    private String title, desc, location, category, price, imageUrl;

    public Event(String title, String imageUrl, String price, String category, String location, String desc) {
        this.title = title;
        this.imageUrl = imageUrl;
        this.price = price;
        this.category = category;
        this.location = location;
        this.desc = desc;
    }
    public Event(){

    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

}

event_list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/request_category"
            android:textStyle="bold"
            android:textSize="15dp"
            android:padding="5dp"
            android:layout_gravity="left"
            android:text="Category will come here"/>

        <TextView
            android:layout_width="191dp"
            android:layout_height="wrap_content"
            android:id="@+id/request_location"
            android:textSize="15dp"
            android:gravity="right"
            android:layout_gravity="right"
            android:text="Location will come here"
            android:padding="5dp"
            android:textStyle="bold"/>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Price will come here"
        android:id="@+id/request_price"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="275dp"
        android:id="@+id/request_image"
        android:src="@drawable/abc_btn_check_material"
        android:adjustViewBounds="true"
        android:clickable="true"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/request_title"
        android:text="Title will come here "
        android:padding="10dp"
        android:textStyle="bold"
        android:textSize="15dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/request_desc"
        android:text="Desc will come here "
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp" />


</LinearLayout>

你快成功了!

一切都很好,除了你的 Switch:在每个 Case 之后,你应该提供一个 Break

现在的写法总是会导致调用最后一个案例,在你的案例中是"Finance"。

为您做的:

switch(view.getId()){
    case R.id.archBttn:
        String arch = "Architecture";
        i.putExtra("Category", arch);
        break;

    case R.id.artBttn:
        String art = "Art";
        i.putExtra("Category", art);
        break;

    case R.id.scienceBttn:
        String science = "Science";
        i.putExtra("Category", science);
        break;

    case R.id.financeBttn:
        String fin = "Finance";
        i.putExtra("Category", fin);
        break;
}

旁注:您的代码写得很漂亮:)

在 FireBaseRecyclerAdapter 中提及数据引用作为参数时,使用如下查询:

databaseReference.orderByChild("category").equalTo(event_cat)

这将仅调用所需的数据类别。