2017-02-07 4 views
0

Ich habe zwei Aktivitäten, eine (MainActivity), wo gibt es eine Option zum Auswählen der Kategorie von Ereignis, die Sie sehen möchten, und die zweite Aktivität (EventView) zeigt Ihnen diese bestimmten Ereignisse basierend auf Ihrer Auswahl. Die Kategorie Optionen sind: Arch Kunst Finanzen WissenschaftFiltern der erforderlichen Daten aus Firebase basierend auf der Schaltflächenauswahl

In der nächsten Aktivität, möchte ich die Daten von Firebase in Form einer Recycler Ansicht kommen auf der Grundlage der Kategorie I wählen, aber alle meine Daten von Firebase wird angerufen. Ich habe eine Abfrage neben meiner DatenbankReferenz hinzugefügt, mit OrderByChild, um meine erforderlichen Daten zu erhalten.

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> 

Antwort

0

Im FireBaseRecyclerAdapter während datareference als Argument zu erwähnen, eine Abfrage wie verwenden:

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

Dadurch wird nur die Kategorie der Daten aufgerufen, die erforderlich ist.

1

Sie fast es!

Alles ist in Ordnung, außer Ihrem Switch: nach jedem Fall Sie eine Pause bieten sollten.

Die Art, wie es jetzt geschrieben wird, wird immer dazu führen, dass der letzte Fall aufgerufen wird, der in Ihrem Fall "Finanzen" ist.

tat dies für Sie:

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; 
} 

Randbemerkung: Code schön geschrieben :)

+0

Hey danke, es hat funktioniert, aber ich habe immer noch Schwierigkeiten, Daten von Firebase zu bekommen, basierend auf welcher Kategorie ich auswähle. Danke für die Randnotiz :) –

+0

Entschuldigung für das fehlende: Ich denke, Ihr Problem kommt aus dieser Zeile des Codes (bei EventView.java): ** if (model.getCategory() == event_cat) ** In Java sollten Sie die **. Equals ** -Funktion für String-Vergleiche verwenden. Ich habe viel Zeit gebraucht, um das selbst herauszufinden .. aber der Operator "==" funktioniert einfach nicht mit Strings. Probieren Sie es aus: ** if (model.getCategory(). Equals (event_cat)) ** – Barackos

+0

Ich habe es versucht, aber es funktioniert noch nicht und zeigt mir die gleiche Layoutdatei von event_list_row.xml –

Verwandte Themen