0

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:

  1. 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?
    
    }
    
  2. 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());
    
    }
    
Harrison
  • 19
  • 5
  • You need `startActivityForResult` - Here is the documentation http://developer.android.com/training/basics/intents/result.html – OneCricketeer Jan 20 '16 at 17:08

2 Answers2

1

You need to use "startActivityForResult()" method. Following link explains perfectly.

How to manage `startActivityForResult` on Android?

Community
  • 1
  • 1
Efe Budak
  • 659
  • 5
  • 27
0

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.

TheoKanning
  • 1,631
  • 11
  • 20
  • I wanted to use a database afterwards. Seems like i have to do that first then? – Harrison Jan 20 '16 at 17:07
  • 1
    That is a bit overkill for something that simply needs `startActivityForResult` – OneCricketeer Jan 20 '16 at 17:08
  • Agreed. It depends what you want to do with the data, if you want to store it eventually, then it can make sense to store it as soon as the user enters it, but if you only want it temporarily, you should use `startActivityForResult` and end the `MainAddMedActivity` when the user enters data – TheoKanning Jan 20 '16 at 17:12