我正在尝试将文本从自定义对话框传递到家庭活动。捆绑包总是显示为null而不是我尝试传递的值,我不知道为什么。我曾尝试寻找类似的问题,但尚未找到解决方案。
对话
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_ingredients_dialog);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(AddIngredientsDialog.this,
android.R.layout.select_dialog_item, fruits);
editText.setAdapter(arrayAdapter);
text = editText.getText().toString();
Intent intent = new Intent(AddIngredientsDialog.this, AddIngredientsActivity.class);
intent.putExtra("Text", text);
startActivity(intent);
dialog.dismiss();
}
});
}
家庭活动
ingredientList = findViewById(R.id.listView);
ArrayList<String> ingredients = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_ingredients);
//bundle
Bundle extras = getIntent().getExtras();
text = extras.getString("Text");
button = findViewById(R.id.add);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ingredients.add(text);
adapter = new ArrayAdapter<>(AddIngredientsActivity.this, android.R.layout.simple_list_item_1, ingredients);
ingredientList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
最佳答案
在家庭活动中,尝试使用getIntent().getStringExtra("Text")
而不是extras.getString("Text");
//bundle
text = getIntent().getStringExtra("Text")