活动之间传递数据数据

2023-09-03 20:54:43 作者:厕所是最好的保护地

我有以下的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.