R. Jbeily
R. Jbeily

Reputation: 7

Get information from clicked cardview into an Activity

Currently i am developing an event app, and my events are shown in card views inside a recycler view, and when i press a card view an activity opens to show more details of that specific event. now the problem i am facing is how to get these information for that event and put them into the activity, and there are other infos that i need to get from parse database. the card view only hold several ones.

if anyone can help, and thank you in advance.

here is my EventAdapter class

public class EventAdapter extends RecyclerView.Adapter {

private TextView eventName;
@NonNull
private final Context context;
@NonNull
private final List<Events> eventsList;

public EventAdapter(@NonNull Context context,@NonNull List<Events> eventsList) {
    this.context = context;
    this.eventsList = eventsList;
}

@Override
public EventViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_item_card_layout,parent,false);
    return new EventViewHolder(view);
}

@Override
public void onBindViewHolder(final EventViewHolder holder, final int position) {
    final Events events = eventsList.get(position);

    holder.locationText.setText(events.getEventCity());
    if (!events.getEventTickets()) {
        holder.costText.setText("Free");
    } else {
        holder.costText.setText(String.format("%.2f", events.getTicketPrice()));
    }


    holder.mainCardLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            eventName = (TextView) view.findViewById(R.id.eventview_name);
            eventName.setText(events.getEventName()); //doesn't work
            view.getContext().startActivity(new Intent(context, EventActivity.class));
        }
    });

Upvotes: 0

Views: 482

Answers (1)

mariosmant
mariosmant

Reputation: 454

In your onClick() method replace the line view.getContext().startActivity(new Intent(context, EventActivity.class));

with the following lines:

Intent i=new Intent(context, EventActivity.class);

//put extras on the intent


i.putExtra("var",myVar); //where myVar is a variable that you would like to pass to the activity.

view.getContext().startActivity(i);

Now, on the EventActivity, inside the

onCreate()

method do the following to retrieve the variable with key "var":

Intent intent = getIntent();

String myVar = intent.getStringExtra("var");

You can pass anything, String, int, bool etc. As an example, I used String.

Upvotes: 1

Related Questions