Home » Android » java – mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false); not working-Exceptionshub

java – mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false); not working-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:
/*JAVA File*/ArrayList<UserObject> userList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_user);

    initializeRecyclerView();

}

private void initializeRecyclerView() {
    mUserList=findViewById(R.id.userList);
    mUserList.setNestedScrollingEnabled(false);
    mUserList.setHasFixedSize(false);
    mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false);
    mUserList.setLayoutManager(mUserListLayoutManager);
    mUserListAdapter =new UserList(userList);
    mUserList.setAdapter(mUserListAdapter);
}

XML File:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FindUserActivity">


<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/userList"
    android:scrollbars="vertical">

</androidx.recyclerview.widget.RecyclerView>

The Line : mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false);

Is providing a warning :Must be One of RecyclerView.HORIZONTAL OR RecyclerView.VERTICAL
Issue ID:WrongConstant

How to&Answers:

You should use constants, defined in RecyclerView for orientation:

mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, false);

Answer:

You should use this line:

mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, false);

Then your code must be like this:

private void initializeRecyclerView() {
    mUserList=findViewById(R.id.userList);
    mUserList.setNestedScrollingEnabled(false);
    mUserList.setHasFixedSize(false);
    mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), 
         RecyclerView.VERTICAL, false);
    mUserList.setLayoutManager(mUserListLayoutManager);
    mUserListAdapter =new UserList(userList);
    mUserList.setAdapter(mUserListAdapter);
}