我想把这些数据从当前的activity发送到更多的“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);
}
});
}
您可以使用共享首选项在所有项目中使用数据。你只需要创建一个App首选项类,就像这样:-
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();
}
}
现在只需调用这些方法来保存数据并获取保存的数据。创建一个自己的保存数据和获取保存数据的方法