简体   繁体   English

如何将第二个活动中的RecyclerView项目传递给Android中的上一个活动?

[英]How do I pass a RecyclerView item in the second Activity to the previous Activity in Android?

I have 2 activities - A and B 我有2个活动-A和B

A- Detail Activity where details get updated A- Detail Activity ,详细信息会更新

B- A Search Activity or Fragment where the user selects an item from a list of items and the selected item is reflected in Activity A B- Search ActivityFragment ,其中用户从项目列表中选择一个项目,并且所选项目反映在活动A中

Can anyone suggest a good and efficient way to achieve this functionality? 谁能建议一种有效的方法来实现此功能?

public interface OnItemClickListener {
    void onItemClick(ContentItem item);
}

Above code in your adapter 适配器中的上述代码

private final List<ContentItem> items;
private final OnItemClickListener listener;

public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
    this.items = items;
    this.listener = listener;
}



@Override public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bind(items.get(position), listener);
}


public void bind(final ContentItem item, final OnItemClickListener listener) {
    ...
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            listener.onItemClick(item);
        }
    });
}


// in your Activity 

recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
    @Override public void onItemClick(ContentItem item) {
        Intent i = new Intent();
        i.putExtra("key","value");
        startActivity(i)
    }
}));

read more 阅读更多

如果将B用作片段,则可以使用接口在片段和活动之间进行通信。片段中的任何更改都可以反映在父活动中。

You can pass the data from one Activity to another using intent. 您可以使用意图将数据从一个活动传递到另一个活动。 Just put the data in bundle or extra when you are calling an intent to navigate through one Activity to another. 当您调用意图通过一个Activity导航到另一个Activity时,只需将数据打包或捆绑在一起。 Like this 像这样

Intent asd = new Intent(getApplicationContext, ActivityA.class);
asd.putExtra(strName, STRING_I_NEED);
startActitvity(asd);

And to get data in ActivityA , 为了在ActivityA获取数据,

Bundle extras = getIntent().getExtras();
if(extras == null) {
     newString= null;
} else {
     newString= extras.getString("STRING_I_NEED");
}

There are two way of doing this 有两种方法

  1. If you wanna just need to pass the data from A to B activity and move forward then you can use the serializable object and pass it along with the intent and afterwards if you return back on the A again then in onResume method you can call the datasource again for the data. 如果您只想将数据从A传递到B并向前移动,则可以使用可序列化的对象并将其与意图一起传递,然后如果再次返回A,则可以在onResume方法中调用数据源再次获取数据。 Link 链接

  2. You can startActivityForResult(); 您可以startActivityForResult(); in activity A and that will starts B Activity and after returning from the B it will automatically trigger onActivityResult(); 在活动A中,它将启动活动B,从活动B返回后,它将自动触发onActivityResult(); Link 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM