Home > Mobile >  I want to use variable in Fragment class to Home class
I want to use variable in Fragment class to Home class

Time:10-26

I want to use postRecyclerView variable in fragment class to home class..

because I want to scroll position when I add post. but in home class I have to define recyclerview. so I plan to use recyclerview variable from fragment class.

Here is home class code.

myRef.setValue(post).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {

            popupClickProgress.setVisibility(View.INVISIBLE);
            popupAddBtn.setVisibility(View.VISIBLE);
            // illegal 오류 해결 수정
            if(popAddPost!=null&&popAddPost.isShowing()){
                popAddPost.dismiss();
            }
            FragmentManager fm = getSupportFragmentManager();
            HomeFragment frag1 = (HomeFragment)fm.findFragmentById(R.id.postRecylerView);
            frag1.



        }
    });

and in fragment class

public class HomeFragment extends Fragment {



private OnFragmentInteractionListener mListener;
private int position;
private int ant;


RecyclerView postRecyclerView ;
PostAdapter postAdapter;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
List<Post> postList;


public HomeFragment() {
    // Required empty public constructor
}

public static HomeFragment newInstance(String param1, String param2) {
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    LinearLayoutManager lin = new LinearLayoutManager(getActivity());
    lin.setStackFromEnd(true);
    lin.setReverseLayout(true);
    View fragmentView = inflater.inflate(R.layout.fragment_home, container, false);
    postRecyclerView = fragmentView.findViewById(R.id.postRV);
    postRecyclerView.setLayoutManager(lin);
    postRecyclerView.setHasFixedSize(true);
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference("Posts");
    return fragmentView;
}

I don't know why I cannot approach postRecyclerView variable??

can you advice for me?

CodePudding user response:

So from my understanding, you are trying to pass the value of that variable ( From Home.java) onto the recycler view which sits inside of a fragment? Is that it?

What you are trying to do is pass data between classes. I'm a bit rusty on Java so this might not be the best way of doing it, but give it a shot and get back to me.

class yourClass {

    private myClassA classAInstance;

    public yourClass(myClassA a)
    {
        classAInstance = a;
    }

    void I_AM_a_button_press()  // function
    {
        classAInstance.printVariable();
    }
}

I also advise you to check this blog post on that matter, it will help you out a lot. Best of luck.

http://www.thetopsites.net/article/54159971.shtml

  • Related