无法使用" startActivityForResult"开始第二项活动。

时间:2015-09-27 16:33:27

标签: android xml android-activity

与标题相同:我已经尝试过" startActivity"但结果相同。当我按下fab按钮时,它会显示HomeActivity。 希望你能帮帮我。

想要从这里发送人员名单

家庭活动:

def recommendations_for_subscriber subscriber, category, language
  course_ids = subscriber.courses.pluck(:id)
  recommendations.reject { |c| course_ids.include? c.id }
end

到这里,修改它并尝试用新人刷新我的RecyclerView 进入它。

SecondActivity:

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);
}

1 个答案:

答案 0 :(得分:1)

用此

替换您的第二个活动代码
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);
}

}