我正在尝试从我正在开始的活动中获取结果。问题是 setResult 不能正常工作:
wortinfo_editsave.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
Intent intent = new Intent();
boolean neu = extras.getBoolean("neu");
if(neu)
{
db.insert_word(wortinfo_wort.getText().toString(),wortinfo_bedeutung.getText().toString(),wortinfo_beispiel.getText().toString(),wortinfo_type.getSelectedItem().toString(),0);
intent.putExtra("res",0);
setResult(100, intent);
finish();
}
else
{
db.update_word(edit_id,wortinfo_wort.getText().toString(),wortinfo_bedeutung.getText().toString(),wortinfo_beispiel.getText().toString(),wortinfo_type.getSelectedItem().toString(),0);
intent.putExtra("res",1);
setResult(100,intent);
finish();
}
}
}
});
我已经多次跟踪程序并且所有条件都正常工作,但仅在一个实例(第一个,neu==true)中,setResult 导致 onActivityResult() 被调用。我还有另一个代码块试图设置结果并完成活动,但它所做的只是关闭活动而不返回任何结果。我也尝试将 100 更改为 RESULT_OK 或其他随机数,但没有奏效:
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.edit_wort:
...
case R.id.delete_wort:
db.delete_wort(wortinfo_wort.getText().toString(), wortinfo_bedeutung.getText().toString());
Intent intent = new Intent();
intent.putExtra("res",2);
setResult(100,intent);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我已经阅读了有关此的所有其他问题,但它有点不同,因为部分代码不起作用,而上面的 if 块工作得很好。