0

im trying to achieve a recycler view in my fragment but these error are not letting me to do this kindly see and tell me

here is my fragment java class

public class HomeFragment extends Fragment {

private HomeViewModel homeViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    homeViewModel =
            ViewModelProviders.of(this).get(HomeViewModel.class);
    View root = inflater.inflate(R.layout.fragment_home, container, false);

    MyListData[] myListData = new MyListData[] {
            new MyListData("Email", android.R.drawable.ic_dialog_email),
            new MyListData("Info", android.R.drawable.ic_dialog_info),
            new MyListData("Delete", android.R.drawable.ic_delete),
            new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),
            new MyListData("Alert", android.R.drawable.ic_dialog_alert),
            new MyListData("Map", android.R.drawable.ic_dialog_map),
            new MyListData("Email", android.R.drawable.ic_dialog_email),
            new MyListData("Info", android.R.drawable.ic_dialog_info),
            new MyListData("Delete", android.R.drawable.ic_delete),
            new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),
            new MyListData("Alert", android.R.drawable.ic_dialog_alert),
            new MyListData("Map", android.R.drawable.ic_dialog_map),
    };
    RecyclerView recyclerView = root.findViewById(R.id.recyclerView);
    MyListAdapter adapter = new MyListAdapter(myListData);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
    return root;
   }
}

i am error on these lines RecyclerView recyclerView = root.findViewById(R.id.recyclerView) and recyclerView.setLayoutManager(new LinearLayoutManager(this));

here is my fragment view model class

public class HomeViewModel extends ViewModel {

  private MutableLiveData<String> mText;

  public HomeViewModel() {
      mText = new MutableLiveData<>();
      //mText.setValue();
  }

  public LiveData<String> getText() {
      return mText;
   }
}

here is my xml

<androidx.recyclerview.widget.RecyclerView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/t1"
                android:scrollbars="vertical"
                android:id="@+id/recyclerView"
                />
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52

2 Answers2

0

Try this,

RecyclerView recyclerView = root.findViewById(R.id.recyclerView); 
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
Rcv_Results.setLayoutManager(mLayoutManager);
Sajith
  • 713
  • 6
  • 21
0
You have to use 

recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

instead of

recyclerView.setLayoutManager(new LinearLayoutManager(this));
SAVALIYA REENA
  • 218
  • 2
  • 15