0

I am making a Grocery list app.

In my first Activity I have a ListView and in my second activity I can add the new grocery details. I want to show those details in the first Activity's ListView. Now how can I pass data from second Activity to the first Activity in ListView.

In second activity i passed data with fooling code:

            EditText editName = (EditText) findViewById(R.id.txtName);
             EditText editQty=(EditText) findViewById(R.id.txtqty);
             String name= editName.getText().toString();
             String quantity=editQty.getText().toString();
             Intent returnIntent = new Intent();
             returnIntent.putExtra("name",name);
             returnIntent.putExtra("quantity",quantity);
             setResult(RESULT_OK,returnIntent);
             finish();

And in first activity i used that Intent as follows:

           protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == RESULT_OK){
                Bundle b = data.getExtras();
                if ( b!= null ){
                String strName=data.getStringExtra("name");
                String strQty=data.getStringExtra("quantity");
                System.out.println(strName);
                System.out.println(strQty);

                }
            }
            if (resultCode == RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }

Please help me solve this. Thanks in advance.

Developer
  • 1,435
  • 2
  • 23
  • 48

3 Answers3

0

I suggest you to use SQLite for your issue, when you insert new grocery details, just create one table and insert that details in database table. Now , when you want to display that newely added details, just run "select" query and set your ListView Adapter.

If everytime you do not want to set your adapter, then just maintain one boolean static variable like isGroceryAdded. If grocery details added successfully, at that time make that boolean variable "true". And check in your first activity that is that variable is true, then run query again and set your adapter again, after setting the adapter, just make that variable false again.

Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
0

Without having some code, my suggestion would be to create a model class that implements serializable, put that in the intent as a result and pass it back to the first Activity (note for this to work, the second activity has to be started with startActivityForResult()).

For a similar question, please have a look at https://stackoverflow.com/a/14333555/1082942

Community
  • 1
  • 1
Andrew Breen
  • 685
  • 3
  • 11
0

This is a general overview of one solution that should work in most cases. Considering you didn't provide code specific to your situation this may or may not be what you need.

1) To resume the first activity, send an intent from the second activity to the first activity containing the grocery details as intent extras (key-value pairs).

2) Override the first activity's onResume() method to handle the incoming intent to pull out the data you want.

3) Then you need to update the data used by your adapter with this intent extra information.

4) Then you need to call notifyDataSetChanged() on your adapter so it will recreate its views with the updated info

Hope you find this helpful.