活动之间传递数据 [英] Passing data between activities

查看:92
本文介绍了活动之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

chart.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    final String aux= (String) lt.getItemAtPosition(position);
        Intent myIntent = new Intent(infoList.this, tabList.class);
        startActivity(myIntent);
    }
});

我也有一个的ListView 。当我点击从的ListView项目我导航到另一个活动,显示了我该活动的信息。我怎么做?我希望这些信息对应于我点击的项目。

I also have a ListView. When I click on an item from that ListView I navigate to another activity that shows me the info for that activity. How do I do that? I want that info to correspond to the item I clicked.

推荐答案

下面是我在这种情况下做了,如果你不想只是通过一个ID后面:

Here is what I did in this situation, if you don't want to just pass an id back:

我所说的其他活动有这样的:

I call the other activity with this:

            Intent intent = new Intent(myapp, CreateExerciseActivity.class);
            intent.putExtra("selected_id", workout.getId());
            startActivityForResult(intent, CREATE_ACTIVITY);

我那么做

            Intent returnIntent = new Intent();
            returnIntent.putExtra("name", m.getName());
            returnIntent.putExtra("unit", m.getUnit());
            returnIntent.putExtra("quantity", m.getQuantity());
            if (getParent() == null) {
                setResult(Activity.RESULT_OK, returnIntent);
            } else {
                getParent().setResult(Activity.RESULT_OK, returnIntent);
            }
            finish();

所以,在这种情况下,我传递一个id,以获得来自用户的更多详细信息,然后我通过这些返回给调用活动,为了增加它,因为我不想保存此直到用户选择购买。

So, in this case I was passing in an id in order to get more details from the user, and then I pass those back to the calling activity, in order to add it, as I didn't want to save this until the user chooses to later.

所以,你需要做的 startActivityForResult ,以便让它能够返回数据。

So, you need to do startActivityForResult in order to have it able to return the data.

这篇关于活动之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆