Reputation: 19
I have a Problem with my thinking. I have 2 Actvities:
As you can see there are EditText Widgets in the second Activity. I want now that the content of them gets saved in the ListView of the MainActivity by pressing the floating action button in MainAddMedActivity.
I tried to use an adapter but i couldn't figure it out. Hope you can help me. I am using an extra xml for the layout in the list.
This is so far what I have:
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), MainAddMedActivity.class);
startActivity(intent);
}
});
ListView mainListView = (ListView) findViewById(R.id.listView);
listAdapter = new ArrayAdapter<String>(this, R.layout.single_row);
mainListView.setAdapter(listAdapter);
String bIntent = getIntent().getExtras().getString("CONTENT_SEARCH_MED"); //And now?
}
MainAddMedActivity
private EditText txtSearchMedicament;
private EditText txtNumberMedicament;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_add_med);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText description = (EditText) findViewById(R.id.txtSearchMedicament);
String text = description.getText().toString();
// Now what?!
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.txtSearchMedicament = (EditText) findViewById(R.id.txtSearchMedicament);
this.txtNumberMedicament = (EditText) findViewById(R.id.txtNumberMedicament);
Intent i = new Intent(this, MainActivity.class);
i.putExtra("CONTENT_SEARCH_MED", txtSearchMedicament.getText().toString());
i.putExtra("CONTENT_MED_NR", txtNumberMedicament.getText().toString());
}
Upvotes: 0
Views: 219
Reputation: 659
You need to use "startActivityForResult()" method. Following link explains perfectly.
How to manage `startActivityForResult` on Android?
Upvotes: 1
Reputation: 1639
Only one Activity
can be active at a time, so you'll have to store the data in SharedPreferences
or a database and reload it when MainActivity
resumes.
Upvotes: 0