我有startActivityForResult()调用一个名为FlavorList的活动,它是一个从数据库填充的列表视图。我想,当我点击列表中的某个项目以使其返回到使用该数据调用它的活动(EditRecipe)时。从EditRecipe中的方法showAddFlavorDialog()调用startActivityForResult(),它打开一个警告对话框。当我点击FlavorList中的项目时,它只是坐在那里并且什么都不做
EditRecipe中的showAddFlavorDialog()调用startActivityForResult()和onActivityResult:
protected void showAddFlavorDialog(){
LayoutInflater layoutInflater = LayoutInflater.from(EditRecipe.this);
View promptView = layoutInflater.inflate(R.layout.add_recipe_flavor_alert, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EditRecipe.this);
alertDialogBuilder.setView(promptView);
final EditText percentageET = (EditText) promptView.findViewById(R.id.is_flavorpercentage);
final TextView flavorNameTV = (TextView) promptView.findViewById(R.id.txt_flavor_name);
final TextView flavorVendorTV = (TextView) promptView.findViewById(R.id.txt_flavor_vendor);
final LinearLayout flavorLayout = (LinearLayout) promptView.findViewById(R.id.add_flavor_layout);
flavorLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(EditRecipe.this, FlavorList.class);
request_Code = 1;
startActivityForResult(i, request_Code);
if(tempFlavorName != null) {
flavorNameTV.setText(tempFlavorName);
flavorVendorTV.setText(tempFlavorVendor);
}
}
});
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
addFlavorAlert = alertDialogBuilder.create();
addFlavorAlert.show();
}//end showAddFlavorDialog
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code)
{
if(resultCode == RESULT_OK)
{
Bundle extras = getIntent().getExtras();
tempFlavorName = extras.getString("FLAVOR_NAME");
tempFlavorVendor = extras.getString("FLAVOR_VENDOR");
tempFlavorWeight = extras.getDouble("FLAVOR_WEIGHT");
}
}
}
FlavorList中listview的监听器。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
_id = dbManager.getFlavorID(cursor);
flavorName = dbManager.getFlavorName(cursor);
flavorVendor = dbManager.getFlavorVendor(cursor);
flavorBase = dbManager.getFlavorBase(cursor);
flavorWeight = String.valueOf(dbManager.getFlavorWeight(cursor));
int callingActivity = getIntent().getIntExtra("CALLING_ACTIVITY", 0);
switch (callingActivity) {
case (Constants.FROM_MAIN):
showModifyDialog();
break;
case (Constants.FROM_EDIT_RECIPE):
Intent returnIntent = new Intent();
returnIntent.putExtra("FLAVOR_NAME", flavorName);
returnIntent.putExtra("FLAVOR_VENDOR", flavorVendor);
returnIntent.putExtra("FLAVOR_WEIGHT", flavorWeight);
setResult(RESULT_OK, returnIntent);
finish();
break;
}//end switch-case
}
});
答案 0 :(得分:0)
为此你需要将你的活动放在你的意图中。
Intent returnIntent = new Intent(context, EditRecipe.class);
目前您只创建了一个意图Intent returnIntent = new Intent();
,在它知道要去哪里之前无效。
答案 1 :(得分:0)
您的@Override
方法没有onActivityResult
注释。
添加它。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
使用getIntent
而非创建新意图。
Intent returnIntent = getIntent();
答案 2 :(得分:0)
Intent
,
Intent returnIntent = new Intent(classname.this, EditRecipe.class);
如果您查看onResultActivity
的参数,它已经为您提供了生成活动的意图,您不必创建一个,只需使用该活动。
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code)
{
if(resultCode == RESULT_OK)
{
// Bundle extras = getIntent().getExtras();
tempFlavorName = data.getString("FLAVOR_NAME");
tempFlavorVendor = data.getString("FLAVOR_VENDOR");
tempFlavorWeight = data.getDouble("FLAVOR_WEIGHT");
}
}
}
快乐编码..
答案 3 :(得分:0)
当你表达意图时,Intent i = new Intent(EditRecipe.this,FlavorList.class); ,你没有在意图中添加Extra。然后在FlavorList中你要求额外的“CALLING_ACTIVITY”。 int callingActivity返回零,因为intent没有,并且无法使用键CALLING_ACTIVITY找到Extra。所以你的switch语句与零匹配并且不合适。尝试使用方法putExtra()。