0

I have a button to add a task .. when I press on it , it takes me to another activity to fill the task details such as (task name, desc, duedate..etc) and when I press on save it must transfer just the task name to the first activity to save it in a list view. I don't have any errors but the (Unfortunately, the application has stopped ) occurs at run time error. please help me !!

public class MainActivity extends AppCompatActivity {

    Button add;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = (Button) findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(v.getContext(),addtask.class);
                startActivityForResult(i,0);
            }


        });

        Intent intent= getIntent();
        String task= intent.getStringExtra("task");


        ArrayList<String> mylist=new ArrayList<String>();
        ListView lv=(ListView) findViewById(R.id.listview);

        mylist.add(task);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.activity_main,R.id.listview, mylist);
        lv.setAdapter(adapter);

    }}

and my second class (activity2)

public class addtask extends Activity {
    Button save;
    EditText tasktxt;
    public static String task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dolist);
        tasktxt= (EditText) findViewById(R.id.tasktxt);
        task= tasktxt.getText().toString();
        save= (Button) findViewById(R.id.save);

        save.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(addtask.this,MainActivity.class);// define the intent
                intent.putExtra("task",task); // store the message in "message"
                startActivityForResult(intent,0); // calling the other activity
            }
        });

    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
NAZXD
  • 17
  • 1
  • 6
  • Please [edit] your question to include the logcat if your app crashes – OneCricketeer Jun 17 '16 at 16:18
  • And `tasktxt.getText().toString()` should be **inside** the `onClick`, otherwise, you have an empty String – OneCricketeer Jun 17 '16 at 16:19
  • sorry but I very beginner in android :D another question please,, if I want to save the results in the listview so that if I close the application and open it the results will be the same what should I use ? store them in a db or use shared preferences or what !!? – NAZXD Jun 18 '16 at 07:23
  • SharedPreferences aren't designed to store lists, so I think that answers your question – OneCricketeer Jun 18 '16 at 12:14

3 Answers3

0

You should override onActivityResult in MainActivity.

And intead of startActivityForResult() use setResult(Activity.RESULT_OK, intent); in addtask

Alexander
  • 857
  • 6
  • 10
0

In your addtask Activity you should do:

save.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        Intent returnIntent = new Intent();
        returnIntent.putExtra("task",task); // store the message in "message"
        setResult(Activity.RESULT_OK,returnIntent);
        finish();
    }
});

Then in your MainActivity you should override onActivityResult (you will get the intent from addtask there):

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

    if (requestCode == 0) {
        if(resultCode == Activity.RESULT_OK){
            // You can remove this code from onCreate
            String task= data.getStringExtra("task");

            ArrayList<String> mylist=new ArrayList<String>();
            ListView lv=(ListView) findViewById(R.id.listview);
            mylist.add(task);
            ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.activity_main,R.id.listview, mylist);
            lv.setAdapter(adapter);
        }
    }
}
Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
0

I think your First Activity can be like this:

   Button add;
   ListView lv;
   ArrayList<String> mylist=new ArrayList<String>();
   ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = (Button) findViewById(R.id.add);
        lv=(ListView) findViewById(R.id.listview);

        adapter=new ArrayAdapter<String>   
        (MainActivity.this,R.layout.activity_main,R.id.listview, mylist); 
        lv.setAdapter(adapter);


        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(v.getContext(),addtask.class);
                startActivityForResult(i,0);
           });
    }

    @Override  //to receive result 
    protected void onActivityResult(int requestCode, int resultCode, Intent    
    data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 0) {
                if (resultCode == RESULT_OK) {
                    //Use data intent to get string
                    String task = data.getStringExtra("task");

                    //adding new task
                    mylist.add(task);
                    arrayAdapter.notifyDataSetChanged();
                }
            }
        }
    }

And the Second Activity be like this:

    public class addtask extends Activity {
    Button save;
    EditText tasktxt;
    public static String task;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dolist);
        tasktxt= (EditText) findViewById(R.id.tasktxt);
        task= tasktxt.getText().toString();
        save= (Button) findViewById(R.id.save);

        save.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(addtask.this,MainActivity.class);
                //define the intent
                intent.putExtra("task",task); 

                setResult(RESULT_OK, intent); 
                finish();
            }
        });
    }

In the Frist Activity you must override the onActivityResult function as this function is called by the android system when you return data from second activity to the first one .

In the Second Activity you call setResult function to set result code that will be checked in the first activity, and call finish() to remove this activity from the activity stack and go back to the first one.

**I'm not sure if there is an error in your array adapter instantiation or not.*

Ahmed Nasr
  • 26
  • 4
  • Thank u sooooooooooo much !! that works with me perfectly ! – NAZXD Jun 17 '16 at 20:38
  • @NAZXD You are welcome :) , This may be helpful for your future questions http://stackoverflow.com/help/someone-answers – Ahmed Nasr Jun 18 '16 at 00:12
  • Naser ,, sorry bit I very beginner in android :D another question please,, if I want to save the results in the listview so that if I close the application and open it the results will be the same what should I use ? store them in a db or use shared preferences or what !!? – NAZXD Jun 18 '16 at 06:38
  • Comparison: http://stackoverflow.com/questions/6276358/pros-and-cons-of-sqlite-and-shared-preferences Shared preferences tutorial: http://www.compiletimeerror.com/2015/02/android-shared-preferences-example-and-tutorial.html#.V2Uwqfl97IU – Ahmed Nasr Jun 18 '16 at 11:39