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);
}
});
}