Search code examples
android-recyclerviewcustom-adapteronactivityresult

RecyclerView with in recyclerView with start ActivityOnresult


I have a recycler view(A) containing another recyclerview(B). When I click on the plus icon in recyclerview(A) item, it opens up a new activity with a recyclerview(C). Long pressing and selecting few items and clicking on done. This needs to update a recyclerview(B) on recyclerview (A) item on which I clicked the plus icon.

But instead, all the items of recyclerview(A) which contains recyclerview(B) is being updated.

private void displayUpcomingEvents(View view) {
    upcomingEventsArrayList = new ArrayList<>();
    upcomingEvents = new UpcomingEvents("Meet and greet", "Coffee Connect | ", "Phython", "21 - ", "Jun - ", "22", "Jun", "21 Jun 10 AM", "Washinton");
    upcomingEventsArrayList.add(upcomingEvents);
    upcomingEvents = new UpcomingEvents("Meet and greet", "Coffee Connect | ", "Phython", "21 - ", "Jun - ", "22", "Jun", "21 Jun 10 AM", "Washinton");
    upcomingEventsArrayList.add(upcomingEvents);
    upcomingEvents = new UpcomingEvents("Meet and greet", "Coffee Connect | ", "Phython", "21 - ", "Jun - ", "22", "Jun", "21 Jun 10 AM", "Washinton");
    upcomingEventsArrayList.add(upcomingEvents);
    upcomingEventsAdapter = new RecyclerAdapterUpcomingEvents(getContext(), upcomingEventsArrayList);

    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_upcoming_event);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getAppContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(upcomingEventsAdapter);

}



class RecyclerAdapterUpcomingEvents extends RecyclerView.Adapter<RecyclerAdapterUpcomingEvents.MyViewHolder> {

    private List<UpcomingEvents> upcomingEventsList;
    private Context context;
    private LayoutInflater inflater;


    RecyclerAdapterUpcomingEvents(Context context, List<UpcomingEvents> upcomingEventsList) {
        this.context = context;
        this.upcomingEventsList = upcomingEventsList;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View rootView = inflater.inflate(R.layout.card_view_upcoming_event, parent, false);
        return new RecyclerAdapterUpcomingEvents.MyViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        UpcomingEvents upcomingEvents = upcomingEventsList.get(position);

        holder.eventUpcomingName.setText(upcomingEvents.getEventUpcomingName());
        holder.eventUpcomingType.setText(upcomingEvents.getEventUpcomingType());
        holder.eventUpcomingDate.setText(upcomingEvents.getEventUpcomingDateStart() + upcomingEvents.getEventUpcomingDateEnd());
        holder.eventUpcomingMonth.setText(upcomingEvents.getEventUpcomingMonthEnd() + upcomingEvents.getEventUpcomingMonthEnd());
        holder.eventUpcomingAlarmNotify.setText(upcomingEvents.getEventUpcomingAlarmNotify());
        holder.eventUpcomingPlace.setText(upcomingEvents.getEventUpcomingPlace());

        holder.eventUpcomingAddPeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity origin = (Activity) context;
                origin.startActivityForResult(new Intent(getAppContext(), ContactsActivity.class), CONTACT_CODE);
            }
        });


        holder.contactRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        contactAdapter = new ContactAdapter(getAppContext(), R.layout.card__contact_image, contactsList);`enter code here`
        holder.contactRecyclerView.setAdapter(contactAdapter);

    }


    @Override
    public int getItemCount() {
        return upcomingEventsList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView eventUpcomingName, eventUpcomingType, eventUpcomingDate, eventUpcomingMonth, eventUpcomingAlarmNotify, eventUpcomingPlace, eventUpcomingYes, eventUpcomingInterested, eventUpcomingNo;
        private ImageView eventUpcomingAddPeople, eventUpcomingmore;
        private RecyclerView contactRecyclerView;


        MyViewHolder(View itemView) {
            super(itemView);
            eventUpcomingName = (TextView) itemView.findViewById(R.id.event_name);
            eventUpcomingType = (TextView) itemView.findViewById(R.id.event_type);
            eventUpcomingDate = (TextView) itemView.findViewById(R.id.event_date);
            eventUpcomingMonth = (TextView) itemView.findViewById(R.id.event_month);

            eventUpcomingAlarmNotify = (TextView) itemView.findViewById(R.id.event_time_notify);
            eventUpcomingPlace = (TextView) itemView.findViewById(R.id.event_place);

            eventUpcomingYes = (TextView) itemView.findViewById(R.id.event_yes);
            eventUpcomingInterested = (TextView) itemView.findViewById(R.id.event_interested);
            eventUpcomingNo = (TextView) itemView.findViewById(R.id.event_no);

            eventUpcomingmore = (ImageView) itemView.findViewById(R.id.event_more);
            eventUpcomingAddPeople = (ImageView) itemView.findViewById(R.id.hex_one_event);

            contactRecyclerView = (RecyclerView) itemView.findViewById(R.id.upcome_events_recycler);

        }
    }
}



@Override
public void onReceived(int requestCode, int resultCode, Intent data) {

    if (requestCode == CONTACT_CODE && resultCode == RESULT_OK && data != null) {

        List<Contact> contactsLists = (ArrayList<Contact>) data.getSerializableExtra("contacts");
        contactsList.clear();
        contactsList.addAll(contactsLists);

        contactAdapter.setContactList(contactsLists);

        upcomingEventsAdapter = new RecyclerAdapterUpcomingEvents(getContext(), upcomingEventsArrayList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getAppContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(upcomingEventsAdapter);
    }
}

CardView

        <android.support.v7.widget.RecyclerView
            android:id="@+id/upcome_events_recycler"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_below="@id/event_place"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="52dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp">

        </android.support.v7.widget.RecyclerView>

        <ImageView
            android:id="@+id/hex_one_event"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentEnd="true"
            android:layout_below="@id/event_place"
            android:layout_margin="8dp"
            android:src="@drawable/hex_src_plus" />

For better understanding


Solution

  • First change your code by this below :

       holder.eventUpcomingAddPeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity origin = (Activity) context;
                Intent i = new Intent(getAppContext(), ContactsActivity.class);
                i.putExtra("selectedPosition", position);
                origin.startActivityForResult(i, CONTACT_CODE);
            }
        });
    
    @Override
    public void onReceived(int requestCode, int resultCode, Intent data) {
    
    if (requestCode == CONTACT_CODE && resultCode == RESULT_OK && data != null) {
        int result = data.getIntExtra("selectedPosition","put a default value in case of null");
        List<Contact> contactsLists = (ArrayList<Contact>) data.getSerializableExtra("contacts");
        contactsList.clear();
        contactsList.addAll(contactsLists);
    
        contactAdapter.setContactList(contactsLists);
         upcomingEventsAdapter.notifyItemChanged(result);
    }
    }
    

    on the second activity you should have something like that when you click on done :

    int param = getIntent().getIntExtra("selectedPosition");
    Intent resultIntent = new Intent();
    resultIntent.putExtra("contacts", Your selected contacts);
    resultIntent.putExtra("selectedPosition", param);
    
    setResult(RESULT_OK, resultIntent);
    finish();
    

    Hope it's clear and helpful :)