How do I pass the correct context to the new RelativeLayout? android java

heres my function

public void populateList(View v){
    ViewGroup LLview = (ViewGroup) v.findViewById(R.id.LinearLayout);
    RelativeLayout row1 = new RelativeLayout(this);
    LLview.addView();

}

View v = inflater.inflate(R.layout.fragment_lesson_list, container, false);
    populateList(v);

      

'this' on line 3 throws error: RelativeLayout (android.context.Content) in relative layout cannot be applied to (ian.marxbrothers.LessonList)

How can I create a new RelativeLayout? i just want to add it to LLview

this method is in the LessonList fragment class, which eventually becomes included in the MainMenu Activity after the adapter.

Thanks for any help. ive got stuck trying this for hours.

+3


source to share


2 answers


public void populateList(View v){
ViewGroup LLview = (ViewGroup) v.findViewById(R.id.LinearLayout);
RelativeLayout row1 = new RelativeLayout(getActivity().getApplicationContext());
LLview.addView();
}

      



This usually works for me or getActivity().getBaseContext();

+3


source


If you are inside a fragment, use getActivity()

to get context.



public void populateList(View v){
ViewGroup LLview = (ViewGroup) v.findViewById(R.id.LinearLayout);
RelativeLayout row1 = new RelativeLayout(getActivity());
LLview.addView();

}

      

+1


source







All Articles