3

Same as title: i already tried with "startActivity" but same result. When i press the fab button it shows the HomeActivity. Hope you can help me.

Want to send the list of person from here

Home Activity:

public class HomeActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public ArrayList result = new ArrayList();

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 0 && resultCode == RESULT_OK && data!=null){
        result = data.getParcelableArrayListExtra("list_modified");
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mRecyclerView = (RecyclerView) findViewById(R.id.rv);

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setItemAnimator(new ScaleInBottomAnimator());

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
    mAdapter = new MyAdapter(createList(0));
    mRecyclerView.setAdapter(mAdapter);
    fab.attachToRecyclerView(mRecyclerView);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(HomeActivity.this, CreateNewPerson.class);
            i.putExtra("list", result);
            startActivityForResult(i, 0);
            mAdapter.notifyDataSetChanged();
        }
    });


}
private List createList(int size) {

    for (int i=1; i <= size; i++) {
        Person p = new Person("Name"+ i, "Surname" + i, R.drawable.ic_action_account_box);
        result.add(p);
    }
    return result;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

to here, modify it and try to refresh my RecyclerView with new person into it.

SecondActivity:

public class CreateNewPerson extends AppCompatActivity {
EditText e1;
EditText e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_person);
    Bundle extra= getIntent().getExtras();
    ArrayList<Person> list = (ArrayList) extra.getParcelableArrayList("list");
    e1 = (EditText)findViewById(R.id.editName);
    e2 = (EditText)findViewById(R.id.editSurname);
    Person p = new Person(e1.toString(), e2.toString(), R.drawable.ic_action_account_box);
    list.add(p);
    Intent output = new Intent();
    output.putExtra("list_modified", list);
    setResult(RESULT_OK, output);
    finish();

}

}
Marco
  • 352
  • 2
  • 14

1 Answers1

1

Replace your second activity code with this

public class CreateNewPerson extends AppCompatActivity {
EditText e1;
EditText e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_person);
    Bundle extra= getIntent().getExtras();
    ArrayList<Person> list = (ArrayList) extra.getParcelableArrayList("list");
    e1 = (EditText)findViewById(R.id.editName);
    e2 = (EditText)findViewById(R.id.editSurname);
    Person p = new Person(e1.toString(), e2.toString(), R.drawable.ic_action_account_box);
    list.add(p);
    Intent output = new Intent();
    output.putExtra("list_modified", list);
    setResult(RESULT_OK, output);
}

}
Faiyaz
  • 574
  • 1
  • 9
  • 39
  • Ok, thank you without "finish()" it works properly. But how can i come back to first activity with new list? Need to implement a button? Can't do it with back button? – Marco Sep 27 '15 at 16:48
  • Yes you need to implement a button. or your can add swipe to refresh in your first activity. – Faiyaz Sep 27 '15 at 16:52