Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to pass some information I have in one fragment, to another fragment.
Basically what I want to do is:
The 1st fragment fetches information from Firebase. When I click on the button of the 1st fragment it passes some data to the 2nd fragment.

Here is what I already did:

What I have tried:

package com.example.espectactorapp.activity.fragment;

public class EmBreveFragment extends Fragment {
private final List\<post\> postList = new ArrayList\<\>();
private AdapterBreve adapterBreve;
private RecyclerView rvPosts;
private ProgressBar progressBar;
private Button buttonRes;
private NavController navController;
private TextView textTitulo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //alterado return por view =
        View rootView = inflater.inflate(R.layout.fragment_em_breve, container, false);
        // inicio
        textTitulo = (TextView) rootView.findViewById(R.id.textTitulo);
        buttonRes = (Button) rootView.findViewById(R.id.ButtonRes);
    
        //ttonRes.setOnClickListener(new View.OnClickListener() {
        //  @Override
        //  public void onClick(View v) {
        //      String titulo = textTitulo.getText().toString();
    
        //      Bundle bundle = new Bundle();
        //      bundle.putString("Titulo", titulo);
    
             //   FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
             //   FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    
            //  ReservasFragment reservasFragment = new ReservasFragment();
            // reservasFragment.setArguments(bundle);
    
            //  fragmentTransaction.replace(R.id.fragment_container, reservasFragment);
            //  fragmentTransaction.commit();
    
        //  }
       //);
        return rootView;
    
        // fim
    
    
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        iniciaComponentes(view);
        configRv();
        recuperaPost();
    }
    private void configRv(){
        rvPosts.setLayoutManager(new LinearLayoutManager(getContext()));
        rvPosts.setHasFixedSize(true);
        adapterBreve = new AdapterBreve(postList);
        rvPosts.setAdapter(adapterBreve);
    
    }
    private void recuperaPost(){
        DatabaseReference postRef = FirebaseHelper.getDatabaseReference()
                .child("posts");
        postRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot ds : snapshot.getChildren()){
                    postList.add(ds.getValue(Post.class));
    
                }
    
                progressBar.setVisibility(View.GONE);
                adapterBreve.notifyDataSetChanged();
    
            }
    
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
    
            }
        });
    
    }
    
    private void iniciaComponentes(View view){
        rvPosts = view.findViewById(R.id.rvPosts);
        progressBar = view.findViewById(R.id.progressBar);
    
    }

}

package com.example.espectactorapp.activity.fragment;

import com.example.espectactorapp.R;

public class ReservasFragment extends Fragment {
// variaveis
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView =  inflater.inflate(R.layout.fragment_reservas, container, false);

        Bundle bundle = getArguments();
        String textTitulo = bundle.getString("Titulo");
    
        TextView firstText = rootView.findViewById(R.id.getTitulo);
    
        firstText.setText(textTitulo);
        
        return rootView;
    
    }

}
Posted
Updated 4-Nov-22 0:19am
Comments
wseng 1-Nov-22 11:36am    
you don't know how to pass data to second fragment?
sergiomendes63 2-Nov-22 6:37am    
Hello,
I used the example above, but I can't get the data through.
Thanks
wseng 2-Nov-22 13:07pm    
When you press the button in the first fragment, are you able to get the data?
sergiomendes63 3-Nov-22 6:07am    
When I press the button, the APP does not run. It shuts down.
wseng 3-Nov-22 13:00pm    
please edit your post by adding the stack trace

1 solution

package com.example.espectactorapp.activity.fragment;

public class EmBreveFragment extends Fragment {
private final List\<post\> postList = new ArrayList\<\>();
private AdapterBreve adapterBreve;
private RecyclerView rvPosts;
private ProgressBar progressBar;
private Button buttonRes;
private NavController navController;
private TextView textTitulo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //alterado return por view =
        View rootView = inflater.inflate(R.layout.fragment_em_breve, container, false);
        // inicio
        textTitulo = (TextView) rootView.findViewById(R.id.textTitulo);
        buttonRes = (Button) rootView.findViewById(R.id.ButtonRes);
    
        //ttonRes.setOnClickListener(new View.OnClickListener() {
        //  @Override
        //  public void onClick(View v) {
        //      String titulo = textTitulo.getText().toString();
    
        //      Bundle bundle = new Bundle();
        //      bundle.putString("Titulo", titulo);
    
             //   FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
             //   FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    
            //  ReservasFragment reservasFragment = new ReservasFragment();
            // reservasFragment.setArguments(bundle);
    
            //  fragmentTransaction.replace(R.id.fragment_container, reservasFragment);
            //  fragmentTransaction.commit();
    
        //  }
       //);
        return rootView;
    
        // fim
    
    
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        iniciaComponentes(view);
        configRv();
        recuperaPost();
    }
    private void configRv(){
        rvPosts.setLayoutManager(new LinearLayoutManager(getContext()));
        rvPosts.setHasFixedSize(true);
        adapterBreve = new AdapterBreve(postList);
        rvPosts.setAdapter(adapterBreve);
    
    }
    private void recuperaPost(){
        DatabaseReference postRef = FirebaseHelper.getDatabaseReference()
                .child("posts");
        postRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot ds : snapshot.getChildren()){
                    postList.add(ds.getValue(Post.class));
    
                }
    
                progressBar.setVisibility(View.GONE);
                adapterBreve.notifyDataSetChanged();
    
            }
    
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
    
            }
        });
    
    }
    
    private void iniciaComponentes(View view){
        rvPosts = view.findViewById(R.id.rvPosts);
        progressBar = view.findViewById(R.id.progressBar);
    
    }

}

package com.example.espectactorapp.activity.fragment;

import com.example.espectactorapp.R;

public class ReservasFragment extends Fragment {
// variaveis
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView =  inflater.inflate(R.layout.fragment_reservas, container, false);

        Bundle bundle = getArguments();
        String textTitulo = bundle.getString("Titulo");
    
        TextView firstText = rootView.findViewById(R.id.getTitulo);
    
        firstText.setText(textTitulo);
        
        return rootView;
    
    }

}
 
Share this answer
 
Comments
Richard Deeming 4-Nov-22 7:11am    
If you want to update your question to add missing information, click the green "Improve question" link and update your question. Do not post your update as a "solution".
sergiomendes63 4-Nov-22 8:37am    
sorry
wseng 4-Nov-22 9:02am    
please paste the stacktrace as well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900