0

I am trying to show the list in RecyclerView. but after setting adapter only Adapter constructor getting called after that nothing happens

Below is the code of Adapter

    public class ChildNameAdpator extends RecyclerView.Adapter<ChildNameAdpator.ViewHolder> {

    List<ChildDatum> arrayList;


    public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
         arrayList = new ArrayList<>();
        arrayList = arrayListChildName;
     }

    @NonNull
    @Override
    public ChildNameAdpator.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
         LinearLayout layoutRowPermissionCount = (LinearLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.row_child_name_layout, parent, false);
         return new ViewHolder(layoutRowPermissionCount);
    }

    @Override
    public void onBindViewHolder(@NonNull ChildNameAdpator.ViewHolder holder, int position) {
         holder.mTvChildName.setText(arrayList.get(position).getChildName());
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        AutoResizeTextView mTvChildName;
        LinearLayout mLinearLayoutChild;
        AutoResizeTextView imageButtonDeleteChildRow;


        ViewHolder(@NonNull View itemView) {
            super(itemView);

            Log.d(TAG, "ViewHolder: ");
            mTvChildName = itemView.findViewById(R.id.tv_row_child_name);
            mLinearLayoutChild = itemView.findViewById(R.id.ll_row_child_name);

        }
    }
}

I am calling this adapter with below code

mRecyclerviewChildName = findViewById(R.id.rv_child_name_parental_control);
    mRecyclerviewChildName.setLayoutManager(new LinearLayoutManager(this));


  mCurrentChildsList.add(new ChildDatum("11", "ty8902", "3333", "3333", "333", "2222", "2222", "1222"));
        mCurrentChildsList.add(new ChildDatum("12", "ty8902", "3333", "3333", "333", "2222", "2222", "1222"));
        mCurrentChildsList.add(new ChildDatum("13", "ty8902", "3333", "3333", "333", "2222", "2222", "1222"));

        childNameAdpator = new ChildNameAdpator(mCurrentChildsList);
        mRecyclerviewChildName.setAdapter(childNameAdpator);

Below is row layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
   <TextView
        android:id="@+id/tv_row_child_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

EDIT: Added Recyclerview's Parent Layout

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <androidx.cardview.widget.CardView
        android:id="@+id/card_view_display_child_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_marginBottom="16dp"
        android:padding="8dp"
        android:visibility="gone"
        app:cardBackgroundColor="@color/colorPrimaryDark"
        app:cardCornerRadius="8dp"
        app:cardElevation="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_child_name_parental_control"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:orientation="vertical">

            </androidx.recyclerview.widget.RecyclerView>

            <com.lb.auto_fit_textview.AutoResizeTextView
                android:id="@+id/ib_add_new_child_name"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginStart="4dp"
                android:layout_marginLeft="4dp"
                android:background="@drawable/ic_add_circle_black_24dp"
                android:padding="8dp"
                android:textAlignment="center"
                android:textColor="@color/colorGreyLight"
                android:textStyle="bold"
                android:visibility="gone" />
        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

After setAdapter only constructor getting called nothing else that's it.

I thought it is due to my Gradle issue and tried on another laptop, the same issue occurred there.

Looking for help Thanks in advance.

5
  • 1
    where you setting LayoutManager like this? RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager);
    – Ali Ahsan
    Commented Aug 22, 2019 at 11:10
  • Why your Adapter class is private? it should be public.
    – Jaymin
    Commented Aug 22, 2019 at 12:09
  • Thanks but no luck, even if I make it public it doesn't matter here, I have tried. Commented Aug 22, 2019 at 12:18
  • add your RecyclerView's parent xml code
    – Ali Ahsan
    Commented Aug 22, 2019 at 12:24
  • Please check updated question Commented Aug 22, 2019 at 12:29

6 Answers 6

1

Change Adapter Constructor and try this

public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
arrayList = new ArrayList<>();
arrayList.addAll(arrayList);
 }

or

public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
this.arrayList = arrayListChildName;
 }
0
0

Try this way:

    rView=findViewById(R.id.rView);
    rView.setHasFixedSize(true);
    lManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    rView.setLayoutManager(lManager);
2
  • Change return new ViewHolder(layoutRowPermissionCount); for return new ChildNameAdpator.ViewHolder(layoutRowPermissionCount);
    – Fustigador
    Commented Aug 22, 2019 at 11:41
  • I have just made these changes but no luck, Commented Aug 22, 2019 at 12:13
0

Make sure the following things you have done:

  1. Declared your adapter class as public

  2. Initialised 'mCurrentChildsList' before adding items.

Initialise: mCurrentChildsList = new ArrayList()

0
0

You have skipped the LayoutManager.

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.

Set the LayoutManager once you initialized the recycler view.

mRecyclerviewChildName.setLayoutManager(new LinearLayoutManager(this));

Reference : LayoutManager

Adapter Code:

public class ChildNameAdpator extends RecyclerView.Adapter<ChildNameAdpator.ViewHolder> {

private List<ChildDatum> arrayList;


public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
    arrayList = arrayListChildName;
}
}

This how your adapter class looks like.

0
0
Main Activity.

    public class DemoTestActivity extends AppCompatActivity {


    RecyclerView mRecyclerviewChildName;
    ChildNameAdpator childNameAdpator;
    List<ChildDatum> mCurrentChildsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_test);

        mCurrentChildsList = new ArrayList<>();
        mRecyclerviewChildName=(RecyclerView)findViewById(R.id.rv_child_name_parental_control);

        mCurrentChildsList.add(new ChildDatum("Mehul"));
        mCurrentChildsList.add(new ChildDatum("Mehul 1"));
        mCurrentChildsList.add(new ChildDatum("Mehul 2"));

        childNameAdpator = new ChildNameAdpator(mCurrentChildsList);
        mRecyclerviewChildName.setLayoutManager(new LinearLayoutManager(DemoTestActivity.this, LinearLayoutManager.VERTICAL, false));
        mRecyclerviewChildName.addItemDecoration(new DividerItemDecoration(mRecyclerviewChildName.getContext(), DividerItemDecoration.VERTICAL));
        mRecyclerviewChildName.setAdapter(childNameAdpator);

    }}


Main Layout file 

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout 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=".DemoTestActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_child_name_parental_control"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="vertical">

    </androidx.recyclerview.widget.RecyclerView>

   </RelativeLayout>


Adapter 

        public class ChildNameAdpator extends 
        RecyclerView.Adapter<ChildNameAdpator.ViewHolder> {

    List<ChildDatum> arrayList;


    public ChildNameAdpator(List<ChildDatum> arrayListChildName) {
        arrayList = new ArrayList<>();
        arrayList = arrayListChildName;
    }

    @NonNull
    @Override
    public ChildNameAdpator.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LinearLayout layoutRowPermissionCount = (LinearLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.row_child_name_layout, parent, false);
        return new ViewHolder(layoutRowPermissionCount);
    }

    @Override
    public void onBindViewHolder(@NonNull ChildNameAdpator.ViewHolder holder, int position) {
        holder.mTvChildName.setText(arrayList.get(position).getChildName());
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView mTvChildName;
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvChildName = itemView.findViewById(R.id.tv_row_child_name);
        }

    }}

Model class  
    public class ChildDatum {

String ChildName;

public ChildDatum(String sChildName) {
    this.ChildName = sChildName;
}

public String getChildName() {
    return ChildName;
}

public void setChildName(String childName) {
    ChildName = childName;
}}
3
  • Share your row_child_name_layout file.
    – MehulGohil
    Commented Aug 22, 2019 at 11:20
  • is it necessary, Adapter is not going beyond Constructor, not even in OnCreateViewHolder Commented Aug 22, 2019 at 11:28
  • Please check it. I have updated my code. It's working for me. Hope its working for you.
    – MehulGohil
    Commented Aug 22, 2019 at 13:02
0

Finally, I got my mistake.

 android:visibility="gone"

VISIBILITY of parent Cardview was GONE due to this onBindViewHolder was not getting called. When I changed the visibility to VISIBLE it Worked.

Thank you all for your efforts.

Not the answer you're looking for? Browse other questions tagged or ask your own question.