Table of Contents
Question content
Workaround
Home Java How to get the data in recycleView's fragment onViewCreated from another fragment?

How to get the data in recycleView's fragment onViewCreated from another fragment?

Feb 11, 2024 pm 05:51 PM

In Android development, Fragment is an important component used to build flexible user interfaces. When using Fragment, sometimes we need to obtain data from one Fragment in another Fragment. Then in the onViewCreated method of the fragment, we can obtain the data in the RecyclerView in other Fragments through the findViewById method. First, we need to make sure that the RecyclerView has been initialized and filled with data before getting the data. Then, we can get the instance of the target Fragment through the findFragmentById method of FragmentManager, and then get the instance of RecyclerView through the findViewById method of the instance. Finally, we can get the data through the RecyclerView adapter. In this way, we can get the data of RecyclerView in another Fragment in the current Fragment. This is a simple and effective method that can help us better handle the transfer and sharing of data in Android development.

Question content

I have fragment workoutfragment and bundle for transferring data to fragment delitefragment:

fragmentmanager fragmentmanager = getfragmentmanager();
 fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();
 fragmenttransaction.replace(r.id.fragment_container, new delitefragment());
 fragmenttransaction.commit();
Copy after login

In the delitefragment, I have an onviewcreated method where the recycler view data call is locked.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    //******************************** getting data from DeliteFragment
    getParentFragmentManager().setFragmentResultListener("text from WOF to AFJ", this, new FragmentResultListener() {
        @Override
        public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle textBundle) {

            Integer number_workout_train_position = textBundle.getInt("text from WOF to AFJ");
            String nameDayTrain  = new   CardSourceImplDayTrain(getActivity()).getCardData(number_workout_train_position).getTitle().toString();

        }
    });

String nameDayTrainRW = nameDayTrain;

         recyclerView = view.findViewById(R.id.train_delite_recycleView);
        cardSource = new CardSourceImplDelite(getActivity().getApplicationContext(),nameDayTrainRW,nameDayTrainRW);
        adapter = new itemAdapterDelite(cardSource);
   }
Copy after login

How to get data in onviewcreated namedaytrain? (so it will go to recyclerview adapter)

Workaround

Use AndroidX view model library to communicate with your activity and fragments. It's very simple✍️

here: AndroidX Jetpack ViewModel

The above is the detailed content of How to get the data in recycleView's fragment onViewCreated from another fragment?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24