稱爲第二個活動我有startActivityForResult()調用稱爲FlavorList的活動,這是從數據庫中填充的列表視圖。我想,當我點擊列表中的一個項目時,它會返回到用數據調用它的活動(EditRecipe)。通過EditRecipe中的方法showAddFlavorDialog()調用startActivityForResult(),該方法打開一個警告對話框。當我點擊在FlavorList的項目,它只是坐在那裏,並在EditRecipe調用startActivityForResult()和onActivityResult什麼也不做從startActivityForResult不打烊
的showAddFlavorDialog():
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.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
}
});
添加日誌你的情況(Constants.FROM_EDIT_RECIPE)之前完成(),並檢查該程序到達那裏與否。 –
當你做出你的意圖時,Intent i = new Intent(EditRecipe.this,FlavorList.class); ,你沒有增加額外的東西。然後在FlavorList中,您要求額外的「CALLING_ACTIVITY」。我認爲int callingActivity返回零,因爲intent找不到CALLING_ACTIVITY。所以你的switch語句不匹配零並且通過。祝你好運!嘗試putExtra()方法。 – flobacca
是的,我忘了做FROM_EDIT_RECIPE情況下的putExtra。在Muddassir指出要檢查它去那裏後,在@flobacca後看到你的評論,但你絕對是對的。 – MarinheiroTX