0

I want to send these data from current activity to more "BusInformationsCard" activity.

@Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {

    viewHolder.busLineName.setText(tickets.get(position).getBusLine());
    viewHolder.seatsNumbers.setText(String.valueOf(tickets.get(position).getSeatNum()));
    viewHolder.leavingTime.setText(tickets.get(position).getLeavingTime());
    viewHolder.companyName.setText(tickets.get(position).getLeavingTime());

    viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // here passing data to BusInformationCard
            Intent ticketInfo = new Intent(mContext, BusInformationsCard.class);

            ticketInfo.putExtra("busLine", tickets.get(position).getBusLine());
            ticketInfo.putExtra("companyName", tickets.get(position).getCompany());
            ticketInfo.putExtra("driverName", tickets.get(position).getName());
            ticketInfo.putExtra("driverPhone", tickets.get(position).getDriverPhone());
            ticketInfo.putExtra("seatNum", tickets.get(position).getSeatNum());
            ticketInfo.putExtra("leavingTime", tickets.get(position).getLeavingTime());
            ticketInfo.putExtra("latitude", tickets.get(position).getLatitude());
            ticketInfo.putExtra("longitude", tickets.get(position).getLongitude());

            mContext.startActivity(ticketInfo);

        }
    });
}
Ali Qadomy
  • 39
  • 5
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – cutiko May 21 '19 at 21:27

3 Answers3

0

you can use shared preference to use your data in all over project. You just need to create an App Preference class like this:-

public class AppPrefrences {

            private static SharedPreferences mPrefs;
            private static SharedPreferences.Editor mPrefsEditor;

            public static boolean isUserLoggedOut(Context ctx) {
                mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
                return mPrefs.getBoolean("id_logged_in", true);
            }

            public static void setUserLoggedOut(Context ctx, Boolean value) {
                mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
                mPrefsEditor = mPrefs.edit();
                mPrefsEditor.putBoolean("id_logged_in", value);
                mPrefsEditor.commit();
            }

    public static String getUserName(Context ctx) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            return mPrefs.getString("userName", "");
        }

        public static void setUserName(Context ctx, String value) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            mPrefsEditor = mPrefs.edit();
            mPrefsEditor.putString("userName", value);
            mPrefsEditor.commit();
        }

public static void clearAllPreferences(Context ctx) {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        mPrefsEditor = mPrefs.edit();
        mPrefsEditor.clear();
        mPrefsEditor.commit();
    }
        }

and now just call these methods for save data and get saved data. create a your own methods for save data and get saved data

Sandeep Malik
  • 1,879
  • 1
  • 6
  • 14
  • leave a comment if you have any query – Sandeep Malik May 22 '19 at 05:40
  • Hey @Sandeep the answer works good but I have a query. What if the data has been changed when we go to a particular activity? we still get the old data which we get from the sharePrefernce right? – Brahma Datta May 22 '19 at 05:57
  • when the data is changed then you have to change your preference data as well otherwise it will always return old data that you have stored before – Sandeep Malik May 22 '19 at 05:58
  • Yeah, so we need to add this set and get methods in the other activities as well to check whether the data has been updated or not right? @Sandeep – Brahma Datta May 22 '19 at 06:01
  • I have seen your answers. You write them very well that everyone can understand @Sandeep. Keep going .!!!! – Brahma Datta May 22 '19 at 06:06
0

You can get your data from one activity to another using Intent's getExtra method

Here is example ::

Example


String name;

name = getIntent().getStringExtra("driverName");

BhavitBJ
  • 22
  • 1
  • 8
0

If you wish to pass the complete ticket object, you can use Serializable or Parcelable class in Java.

These classes help you to convert your object in a form which can be transferred between activities.

All you need to do in case of Serializable is to extend your ticket class

public class Ticket extends Serializable {}

In case of Parcelable, you need to add a little bit more code (in case of Java).

public class Ticket extends Parcelable {

   public void writeToParcel(Parcel dest, int flags) {
      dest.writeString(name);
      dest.writeString(busLine);
      // Similarly for all the other parameters.
   }

   public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public Ticket createFromParcel(Parcel in) {
        return new Ticket(in);
    }
    public Ticket[] newArray(int size) {
        return new Ticket[size];
    }
 };

}

In both these cases, now you can directly pass your complete ticket object in the intent just like intent.putExtra("ticket", ticket); and can receive it in the other activity like, Ticket ticket = getIntent().getSerializableExtra("ticket") */ getParcelable */ if you have extended Parcelable.

The main difference between Parcelable and Serializable is the speed difference, Parcelable is faster than Serializable. Also, parcelable is customisable, and hence developers have the independence to play with their data. Also Serializable used Java Reflection API, and hence ends up with a lot of garbage object during the conversion.