gpt4 book ai didi

android - 无法使用 "startActivityForResult"启动第二个 Activity

转载 作者:数据小太阳 更新时间:2023-10-29 02:39:51 26 4
gpt4 key购买 nike

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

想从这里发送人员名单

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

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

第二个 Activity :

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

}

}

最佳答案

用这个替换你的第二个 Activity 代码

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

}

关于android - 无法使用 "startActivityForResult"启动第二个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32810211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com