Shiv uday Shiv uday - 1 month ago 4
Android Question

Recycler view not showing anything in layout

I am using recycler view but its not showing anything.

Mycode

Recycler view:

<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>


MainActivity:

recyclerView=(RecyclerView)findViewById(R.id.rv);
RecyclerViewLayoutManager=new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(RecyclerViewLayoutManager);
RecyclerView.Adapter adapter=new RecyclerViewAdapter(data,dataname,dataid,datamyname);

recyclerView.setAdapter(adapter);


Recyclerview Adapter:

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


ArrayList<String> data;
ArrayList<String>dataname;
ArrayList<String>dataid;
ArrayList<String>datamyname;

View view1;
String tempname;


public RecyclerViewAdapter(ArrayList<String> data1,ArrayList<String> data2,ArrayList<String>data3,ArrayList<String>data4) {
this.data = data1;
this.dataname = data2;
this.dataid = data3;
this.datamyname = data4;
}

@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view1= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview,parent,false);
return new ViewHolder(view1);

}

@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
holder.tv1.setText(data.get(position));
holder.tv2.setText(dataname.get(position));
tempname=dataid.get(position);
//imag=(ImageView)holder.findViewById(R.id.imageView2);

}

@Override
public int getItemCount() {
return 0;
}

public class ViewHolder extends RecyclerView.ViewHolder{
TextView tv1,tv2,copy,share;
ImageView imag;
public ViewHolder(View itemView) {
super(itemView);
tv1=(TextView)itemView.findViewById(R.id.eventtitle);
tv2=(TextView)itemView.findViewById(R.id.textView5);
imag=(ImageView)itemView.findViewById(R.id.imageView2);
Glide.with(MyApp.getContext()).load(tempname).into(imag);
}
}
}

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="450dp"
xmlns:tools="http://schemas.android.com/tools"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"
android:paddingTop="4dp"
card_view:contentPadding="4dp"
card_view:cardElevation="4dp"
android:id="@+id/card"
card_view:cardCornerRadius="20dp"
tools:context=".BloodDonationHealth">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/rl">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:id="@+id/eventtitle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Description W/khrn_client t.GattService: clientConnect() - tion of void com.yolo.music.YoloInitManager.onApplicationCreate(android.app.Application) took 274.353ms 11-01 11:41:43.087 507-1096/system_process W/art: Long monitor contention event with owner maddress=08:7C:BE:52:00:56, isDirect=false t.GattService: clientConnect() - address=08:7C:BE:52:00:56, isDirect=false init_window window 0x484b3038, 480x800 hintTransform 0x0 do_pre 1 10-31 20:21:04.481 1777-1790/bootcamplearning.humanplanet I/art: Background sticky concurrent mark sweep GC freed 40216(2MB) AllocSpace objects, 2(32KB) LOS objects, 4% free, 26MB/27MB, paused 5."
android:id="@+id/textView5"
android:layout_gravity="left|center_vertical"
android:layout_below="@+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="@+id/imageView2"
android:src="@drawable/globe"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp" />

</RelativeLayout>




<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:src="@drawable/location" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView4"
android:src="@drawable/user"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/share"
android:id="@+id/imageView5" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView6"
android:src="@drawable/chat"/>
</LinearLayout>
</android.support.v7.widget.CardView>

</RelativeLayout>


This is my code,
I couldn't configure whats going wrong,can someone help me to solve this?.
where am i doing wrong,can you please tell me

Answer

RecyclerView is not showing anything for this

@Override
public int getItemCount() {
    return 0;
}

Cz you are telling app that list doesn't have any item to show.

Try like this

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