我的BaseAdapter中的onActivityResult没有触发

时间:2014-08-14 21:00:50

标签: android baseadapter onactivityresult

在我的getView中,我有以下

holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

                switch (v.getId()) {
                    case R.id.ivDelete:

                        PopupMenu popup = new PopupMenu(mContext, v);
                        popup.getMenuInflater().inflate(R.menu.transaction_list_menu,
                                popup.getMenu());
                        popup.show();
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item) {

                                switch (item.getItemId()) {
                                    case R.id.edit:

                                        Intent i = new Intent(v.getContext(), EditAmountDialogActivity.class);
                                        i.putExtra("table", table);
                                        i.putExtra("id", listItemId);
                                        ((Activity) mContext).startActivityForResult(i, Activity.RESULT_OK);

                                        break;
                                    case R.id.delete:

                                        AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
                                        alertDialog.setMessage("Are you sure you want to delete?")
                                                .setCancelable(false)
                                                .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                                                    public void onClick(DialogInterface dialog, int id){
                                                        db.deleteTransaction(table, listItemId);
                                                        notifyDataSetChanged();
                                                    }
                                                })
                                                .setNegativeButton("No", new DialogInterface.OnClickListener(){
                                                    public void onClick(DialogInterface dialog, int id){
                                                        dialog.cancel();
                                                    }
                                                });
                                        AlertDialog alert = alertDialog.create();
                                        alert.show();

                                        break;

                                    case R.id.refresh:
                                        cursor = db.getTransactions(table);
                                        ((TransactionActivity)mContext).setBalances();
                                        notifyDataSetChanged();
                                        break;

                                    default:
                                        break;
                                }

                                return true;
                            }
                        });
在R.id.edit

我正在开始结果活动

在EditAmountActivityDialog.class中的

我有以下

dialogButtonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                memo = etMemo.getText().toString().replace("'", "\'\'");
                timestamp = etDate.getText().toString();


                try {
                    amountDouble = Double.parseDouble(etAmount.getText().toString().replace(",", ""));
                } catch (NumberFormatException e) {

                }

                if (amountDouble == 0.0) {
                    Toast.makeText(getApplicationContext(), "You must enter an amount!",
                            Toast.LENGTH_SHORT).show();
                } else {

                    hideSoftKeyboard();
                    db.updateTransaction(id, table, type, amountDouble, memo, dbTimestamp);
                    db.close();
                    Intent i = new Intent();
                    setResult(RESULT_OK, i);
                    finish();
                }

知道为什么它没有激活onActivityResult?

谢谢!

2 个答案:

答案 0 :(得分:0)

不要startActivityForResult() RESULT_OK作为requestCodeRESULT_OK值为-1,如果onActivityResult()低于0,则requestCode不会被调用。

答案 1 :(得分:0)

您正在使用startActivityForResultsetResult方法错误。

您应该为startActivityForResult指定一个请求代码,以便以后可以在onActivityResult中使用它来确定哪个请求的结果可用。

在班级顶部定义静态和最终变量:

private static final int REQ_EDIT = 100;

它可以是任何东西。我们只是保存它以供以后参考。

然后开始您的活动结果:

 case R.id.edit:
     Intent i = new Intent(v.getContext(), EditAmountDialogActivity.class);
     i.putExtra("table", table);
     i.putExtra("id", listItemId);
     startActivityForResult(i, REQ_EDIT);
     break;

所以你现在已经为结果开始了一个活动。

要使用结果完成活动,请使用以下代码:

...
hideSoftKeyboard();
db.updateTransaction(id, table, type, amountDouble, memo, dbTimestamp);
db.close();
setResult(RESULT_OK);
finish();
...

这会将OK结果返回到您的片段。

在您的片段中,覆盖onActivityResult方法,如下所示:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case REQ_EDIT:
            if(resultCode == Activity.RESULT_OK) {
                // Do something 
            }
            break;
    }
}