2017-06-16 3 views
0

Le bouton devrait ouvrir une nouvelle activité et devrait afficher les informations de cette seule carte particulière dont le bouton a été cliquécliquez sur le bouton dans RecyclerView

J'ai 6 cartes à RecyclerView aussi j'extraire des données de Firebase

Mon code RecyclerView:

public class index extends AppCompatActivity { 

    private Toolbar toolbar; 
    private RecyclerView recyclerView; 
    private DatabaseReference myRef; 

    private FirebaseAuth firebaseAuth; 

    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.index); 

    firebaseAuth = FirebaseAuth.getInstance(); 
    myRef = FirebaseDatabase.getInstance().getReference().child("/Doctor"); 

    if(firebaseAuth.getCurrentUser() == null) 
    { 
     startActivity(new Intent(this, MainActivity.class)); 
    } 

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    FirebaseRecyclerAdapter<Listitem,ListItemHolder> adapter = new 
FirebaseRecyclerAdapter<Listitem, ListItemHolder>(
      Listitem.class, 
      R.layout.list_item, 
      ListItemHolder.class, 
      myRef 
    ) { 
     @Override 
     protected void populateViewHolder(ListItemHolder ItemHolder, 
Listitem model, int position) { 
      ItemHolder.setDocName(model.getDocName()); 
      ItemHolder.setSpeciality(model.getSpeciality()); 
      ItemHolder.setAddress(model.getAddress()); 
      ItemHolder.setExperience(model.getExperience()); 
      ItemHolder.setFees(model.getFees()); 

      ItemHolder.bookButton.setOnClickListener(new 
View.OnClickListener(){ 
       public void onClick(View v){ 
        Intent i = new Intent(index.this, Booking.class); 
        startActivity(i); 

       } 
      }); 
     } 
    }; 

    recyclerView.setAdapter(adapter); 


    } 
    public static class ListItemHolder extends RecyclerView.ViewHolder 
implements View.OnClickListener { 
     TextView head,desc,desc1,desc2,desc3; 
     Button bookButton; 

     public ListItemHolder(View itemView) { 
      super(itemView); 
      itemView.setOnClickListener(this); 
      head = (TextView) itemView.findViewById(R.id.textViewHead); 
      desc = (TextView) itemView.findViewById(R.id.textViewDesc); 
      desc1 = (TextView) itemView.findViewById(R.id.textViewDesc1); 
      desc2 = (TextView) itemView.findViewById(R.id.textViewDesc2); 
      desc3 = (TextView) itemView.findViewById(R.id.textViewDesc3); 

      this.bookButton = (Button) 
itemView.findViewById(R.id.bookButton); 
     } 

     public void setDocName(String DocName) { 
      head.setText(DocName); 
     } 

     public void setSpeciality(String Speciality) { 
      desc.setText(Speciality); 
     } 

     public void setAddress(String Address) { 
      desc1.setText(Address); 
     } 

     public void setExperience(String Experience) { 
      desc2.setText(Experience); 
     } 

     public void setFees(String Fees) { 
      desc3.setText(Fees); 
     } 

     @Override 
     public void onClick(View view) { 

     } 
    } 
} 

CardLayout 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="wrap_content" 
android:background="@color/greyshade" 
android:fitsSystemWindows="true"> 

    <android.support.v7.widget.CardView 
    android:id="@+id/cardView" 
    android:layout_margin="@dimen/activity_horizontal_margin" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp"> 

    <LinearLayout 
     android:padding="@dimen/cardview_default_radius" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:paddingStart="10dp" 
     android:paddingEnd="10dp" 
     > 

     <TextView 
      android:id="@+id/textViewHead" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/Heading" 
      android:textStyle="bold" 
      android:textColor="@color/black" 
      android:textSize="@dimen/text_size" 
      /> 
    <TextView 

    android:id="@+id/textViewDesc" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:text="@string/Desc"/> 

     <TextView 
      android:id="@+id/textViewDesc1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/Desc"/> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      > 
     <TextView 
      android:id="@+id/textViewDesc2" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:textColor="@color/black" 
      android:maxLines="1" 
      android:textSize="12sp" 
      android:text="@string/Desc"/> 

     <TextView 
      android:id="@+id/textViewDesc3" 
      android:layout_width="70dp" 
      android:layout_height="wrap_content" 
      android:textColor="@color/black" 
      android:layout_marginTop="10dp" 
      android:layout_marginStart="5dp" 
      android:textSize="12sp" 
      android:maxLines="1" 

      android:text="@string/Desc"/> 

      <android.support.v7.widget.AppCompatButton 
       android:id="@+id/bookButton" 
       android:layout_width="70dp" 
       android:layout_height="wrap_content" 
       android:text="@string/Book" 
       android:textColor="@color/textbutton" 
       android:textStyle="bold" 
       android:backgroundTint="@color/bookbutton" 
       android:layout_marginStart="130dp" 
       /> 


     </LinearLayout> 

</LinearLayout> 

</android.support.v7.widget.CardView> 

</LinearLayout> 

Mise à jour

<?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"> 

<TextView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/textViewHead" 
android:text="@string/Heading" 
android:layout_marginTop="30dp" 
android:textStyle="bold" 
android:textColor="@color/black"/> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textViewDesc1" 
    android:text="@string/Desc" 
    android:layout_marginTop="20dp" 
    android:textStyle="bold" /> 

    <RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="bottom"> 
    <android.support.v7.widget.AppCompatButton 
    android:id="@+id/logout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/Logout" 
    android:layout_marginBottom="45dp"/> 
    </RelativeLayout> 
    </LinearLayout> 
+0

Quel est exactement le problème? Votre bouton n'ouvre pas l'activité? Les informations que vous souhaitez afficher ne sont pas affichées dans la nouvelle activité? S'il vous plaît élaborer. –

+0

En appliquant l'intention, le bouton ouvre la nouvelle activité mais ne montre pas l'information @antonis_st –

+0

@VismayPatil attendez que je poste la réponse –

Répondre

0

d'abord tout ce que vous devez faire de votre classe modèle sérialisable ...

public class Listitem implements Serializable { ..... } 

Changer votre intention comme ça ..

Intent i = new Intent(index.this, Booking.class); 
i.putExtra("data",model); 
startActivity(i); 

Sur Booking.class méthode actvity onCreate()

Listitem model; 

Intent extra = getIntent(); 
if (extra != null) { 
    model = (Listitem) extra.getSerializableExtra("data"); 
} 
textview.setText(model.getDocName); 
............ 
............ 
+0

Il est l'ouverture de l'activité, mais pas l'affichage des données –

+0

après avoir obtenu la classe de modèle .. afficher vos données .. par 'model.getDocName' etc etc .. –

+0

' ItemHolder.bookButton.setOnClickListener (nouveau View.OnClickListener() { public void onClick (Voir v) { Intention i = nouvelle intention (index.this, Booking.class); i.putExtra ("DocName", model.getDocName()); startActivity (i); } }); ' –

0
ItemHolder.bookButton.setTag(model); 

Et à l'intérieur onClcik

Listitem model= (Listitem) v.getTag(); 

passent ensuite les détails nécessaires à partir du modèle et mis en Bundle, ajouter à l'intention que vous commencez

1

Je vous recommande de jeter un oeil à mon référentiel de projet: Android Firebase Clickable Cards

Il fait exactement ce que vous avez décrit.

Toutefois, si vous ne parvenez qu'à envoyer un objet d'une activité à une autre, la meilleure méthode consiste à utiliser Parcelables.

Faites d'abord votre objet parcelable:

public class Listitem implements Parcelable { 

    private String docName, address, ...; 

    public Listitem() { 
    } 

    public Listitem(String name, String address) { 
     this.docName = name; 
     this.address = address; 
     ... 
    } 

    public String getDocName() { 
     return docName; 
    } 

    public void setDocName(String name) { 
     this.docName = name; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 
    ... 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(this.docName); 
     dest.writeString(this.address); 
     ... 
    } 

    protected Listitem(Parcel in) { 
     this.docName = in.readString(); 
     this.address = in.readString(); 
     ... 
    } 

    public static final Parcelable.Creator<Listitem> CREATOR = new Parcelable.Creator<Listitem>() { 
     @Override 
     public Listitem createFromParcel(Parcel source) { 
      return new Listitem(source); 
     } 

     @Override 
     public Listitem[] newArray(int size) { 
      return new Listitem[size]; 
     } 
    }; 
} 

maintenant pour envoyer objet listItem, il suffit d'ajouter à l'intention:

intent.putExtra("listitem", listitem); 

Pour récupérer l'objet de la classe de réservation:

Listitem listitem = (Listitem) getIntent().getParcelable("listitem"); 
0

Vous devez passer dans le intent les choses que vous voulez montrer dans le nouveau activ ité. Donc, votre code devrait être comme ça.

`Intent i = new Intent(index.this, Booking.class); 
i.putExtra("docName", model.getDocName()); 
i.putExtra("spaecialty", model.getSpeciality()); 
i.putExtra("address", model.getAddress()); 
i.putExtra("experience", model.getExperience()); 
i.putExtra("fees", model.getFees()); 
startActivity(i);` 

et dans votre nouvelle activité quelque chose comme ça

String docName = getIntent().getStringExtra("docName") etc. Notez que le « type supplémentaire » doit correspondre au type de supplémentaire que vous envoyez.

1

Vous devez faire quelques changements dans votre code pour obtenir le résultat que vous voulez t.Si vous voulez aussi la position de la vue que vous avez besoin getAdapterPosition() pour obtenir la position actuelle de CardView que vous avez sélectionné.

Ainsi, votre classe ViewHolder devrait être comme-

public static class ListItemHolder extends RecyclerView.ViewHolder 
implements View.OnClickListener { 
    TextView head,desc,desc1,desc2,desc3; 
    Button bookButton; 

    public ListItemHolder(View itemView) { 
     super(itemView); 
     itemView.setOnClickListener(this); 
     head = (TextView) itemView.findViewById(R.id.textViewHead); 
     desc = (TextView) itemView.findViewById(R.id.textViewDesc); 
     desc1 = (TextView) itemView.findViewById(R.id.textViewDesc1); 
     desc2 = (TextView) itemView.findViewById(R.id.textViewDesc2); 
     desc3 = (TextView) itemView.findViewById(R.id.textViewDesc3); 

     this.bookButton = (Button) itemView.findViewById(R.id.bookButton); 
     bookButton.setOnClickListener(this); // change number 1 
    } 

    public void setDocName(String DocName) { 
     head.setText(DocName); 
    } 

    public void setSpeciality(String Speciality) { 
     desc.setText(Speciality); 
    } 

    public void setAddress(String Address) { 
     desc1.setText(Address); 
    } 

    public void setExperience(String Experience) { 
     desc2.setText(Experience); 
    } 

    public void setFees(String Fees) { 
     desc3.setText(Fees); 
    } 

    @Override 
    public void onClick(View view) { 
     int position = getAdapterPosition(); // change number 2 if you need position for further work. 
     switch(view.getId()){ 
      case R.id.bookButton: 

      // do your other code here 

      break: 
     } 
    } 
    } 
} 

Et pour recevoir des données sur d'autres activités. vous devez faire votre classe de modèle Serializable comme répondu par @Abhishek Singh.