-2

我们有 3 个类 A、B、C。A 类调用 B 类startActivityForResult以从中获取数据。它返回后取决于从B类中检索到的数据,我们是否会去C类获取数据给A类。这将在一个onListItemClick方法中完成。下面的代码不起作用,任何人都可以说出问题所在吗?

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Cursor note = mDbHelper.fetchNote(id);
    startManagingCursor(note);
    mpass= note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_PASS));
    theid = id
    if(mpass !=null) {
        Intent ps = new Intent(this, EnterPass.class); 
        startActivityForResult(ps,2);
    }
    else {
        Intent i = new Intent(this, NoteEdit.class);
        i.putExtra(NotesDbAdapter.KEY_ROWID, id);

        startActivityForResult(i, 1);}
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if(requestCode==2) { 
            if(null!=intent) {
                message=intent.getStringExtra("MESSAGE"); 
                if (mpass == message) { 
                    Intent i = new Intent(this, NoteEdit.class);
                    i.putExtra(NotesDbAdapter.KEY_ROWID, theid);
                    startActivityForResult(i, 1);
                }
            }
            else 
        }
        if(requestCode==1) {
            if (resultCode == RESULT_OK)
                fillData();
        }
4

1 回答 1

0

现在这个答案将出现在 Mono for Android 中,但它仍然适用于常规 Android。

我创建了 3 个非常简单的活动。ActivityA,ActivityBActivityC.

ActivityA我开始ActivityB调用:

var intent = new Intent(this, typeof (ActivityB));
intent.PutExtra("RowId", rowId);
StartActivityForResult(intent, 1);

rowId列表的位置在哪里。您可以通过将更多内容放入Intent.

在其中ActivityB,您可以获取所有这些信息并使用它做任何您想做的事情:

var data = Intent.GetStringExtra("RowId");
var textView = FindViewById<TextView>(Resource.Id.textView1);
textView.Text = data;

这里我只是把它放在 aTextView中来显示rowId传递的 from ActivityA。然后,当我想ActivityB用​​一些数据返回时ActivityA,我会:

var result = new Intent();
result.PutExtra("ActivityBResult", "myResult");
SetResult(Result.Ok, result);
Finish();

这些数据可以在我开始时设置ActivityAOnActivityResult方法look in中case 1获取。然后我开始:RequestCodeStartActivityForResultActivityBActivityC

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (resultCode != Result.Ok) return;
    switch (requestCode)
    {
        case 1:
            {
                var activityBData = data.GetStringExtra("ActivityBResult");
                _textView1.Text = activityBData;

                //Some logic here based on data from ActivityB

                var intent = new Intent(this, typeof(ActivityC));
                intent.PutExtra("ActivityBResult", activityBData);
                StartActivityForResult(intent, 2);
            }
            break;
        case 2:
            {
                var activityCData = data.GetStringExtra("ActivityCResult");
                _textView2.Text = activityCData;
            }
            break;
    }
}

ActivityB以与启动相同的方式ActivityC启动。ActivityC与我返回数据的方式相同ActivityB

var result = new Intent();
result.PutExtra("ActivityCResult", "some result");
SetResult(Result.Ok, result);
Finish();

哪个返回并跳入上面case 2switchOnActivityResult

于 2013-03-24T14:51:33.197 回答