0

hi i want to send some information like name,weight,height,result, by press btnsave in Activity1 to Activity2 and i save them into Recyclerview. I can save them but when i send new informations. . are saved in previous card and replace,i want to have a new card in RecyclerView every time i pressd btnsave in Activity1

my btnsave in Activity1

    btnSave.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                Gson gson = new Gson();
                                ArrayList<Info> bmi = new ArrayList<>();
                                Info info = new Info(name,weight,height,result);
                                bmi.add(info);
                                SharedPreferences sharedPreferences = Activity1.this.getSharedPreferences(DB_KEY,Activity1.this.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString(INFO,gson.toJson(bmi));
                                editor.commit();
                                Intent intent = new Intent(Activity1.this,Activity2.class);
                                startActivity(intent);

                        }
                    });
                }

and this is oncreate in Activity2

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bmi);
    recyclerView = findViewById(R.id.recView);
    adapter  = new Adapter(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));
    initview();
    Gson gson = new Gson();
    SharedPreferences sharedPreferences = Activity2.this.getSharedPreferences(DB_KEY, Activity2.this.MODE_PRIVATE);
    info = gson.fromJson(sharedPreferences.getString(INFO, null), InfoType);
    adapter.setInfo(info);
    if (info != null) {
        view2.setVisibility(View.VISIBLE);
        view1.setVisibility(View.GONE);
        btnBackbmi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(Activity2.this, Activity1.class);
                startActivity(intent1);
            }
        });
    } else {
        view1.setVisibility(View.VISIBLE);
        view2.setVisibility(View.GONE);
        btnstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(Activity2.this, Activity1.class);
                startActivity(intent1);
            }
        });
    }
Ahmad
  • 17
  • 2
  • Why do you want to save data in SharedPref ?? you can simply pass data from activity 1 to another https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – chand mohd Aug 23 '20 at 09:30
  • 2
    Does this answer your question? [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) – Shubham Naik Aug 23 '20 at 09:43
  • i can send info withe bondle or intent or parcelables but my info not create a new Card in RecyclerView. every time i sended info just replace into the one card.and i cant create a new card for new info – Ahmad Aug 24 '20 at 17:50

1 Answers1

0

I think better to use Bundle instead of saving data to shared preference. https://developer.android.com/guide/components/activities/parcelables-and-bundles

Just make your Info - implements Serializable or Parcelable and put it into Bundle

Intent intent = new Intent(this, Activity2.class); 
intent.putExtra("Info_EXTRA", new Info(name, weight, height, result));
startActivity(intent);

and then

 @Override
  protected void onCreate(Bundle savedInstanceState) {
  Info info = (Info) getIntent().getExtras().getSerializable("Info_EXTRA");
  ....
}
  • i can send info with bondle or intent or parcelables but my info not create a new Card in RecyclerView. every time i sended info just replace into the one card.and i cant create a new card for new info. – Ahmad Aug 23 '20 at 15:17
  • So your problem not in data transfer, you need to define data source object, put your info to this data source and then when you are open your activity with recyclerview, get all data from datasource. Cuz for now, every time when you call your activity with recyclerview, its recreated with new data – Legementario Aug 23 '20 at 16:19