When I try to run the app my recycler view unable to generate the output. It is unable to load data and there is no error the code.
Below is adapter class PlayerListAdapter.java
public class PlayerListAdapter extends RecyclerView.Adapter<PlayerListAdapter.FeaturedViewHolder> {
ArrayList<plaerListHelperClass> featuredLocation;
public PlayerListAdapter(ArrayList<plaerListHelperClass> featuredLocation) {
this.featuredLocation = featuredLocation;
}
@NonNull
@Override
public FeaturedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.playerlist,parent,false);
FeaturedViewHolder featuredViewHolder= new FeaturedViewHolder(view);
return featuredViewHolder;
}
@Override
public void onBindViewHolder(@NonNull FeaturedViewHolder holder, int position) {
plaerListHelperClass plaerListHelperClass = featuredLocation.get(position);
String namep= plaerListHelperClass.getPlayerName();
String mailp= plaerListHelperClass.getPlayerEmail();
holder.name.setText(namep);
holder.mail.setText(mailp);
}
@Override
public int getItemCount() {
return featuredLocation.size();
}
public static class FeaturedViewHolder extends RecyclerView.ViewHolder{
TextView name,mail;
public FeaturedViewHolder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.playerName);
mail=itemView.findViewById(R.id.pmail);
}
}
mainactivity.java This is main activity of my code
public class MainActivity extends AppCompatActivity {
RecyclerView featuredRecycler;
RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
featuredRecycler=findViewById(R.id.playerListRecyclerView);
featuredRecycler();
}
private void featuredRecycler() {
featuredRecycler.setHasFixedSize(true);
ArrayList<plaerListHelperClass> featuredLocation = new ArrayList<>();
featuredLocation.add(new plaerListHelperClass("Shubham","[email protected]"));
featuredLocation.add(new plaerListHelperClass("Shubham","[email protected]"));
featuredLocation.add(new plaerListHelperClass("Shubham","[email protected]"));
featuredLocation.add(new plaerListHelperClass("Shubham","[email protected]"));
adapter=new PlayerListAdapter(featuredLocation);
featuredRecycler.setAdapter(adapter);
}
Build this helper class to contains the element that is to be send to the recycler view layout
plaerListHelperClass.java
public class plaerListHelperClass {
String playerName;
String playerEmail;
public plaerListHelperClass(String playerName, String playerEmail) {
this.playerName = playerName;
this.playerEmail = playerEmail;
}
public String getPlayerName() {
return playerName;
}
public String getPlayerEmail() {
return playerEmail;
}
}
playerlist.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
android:id="@ id/playerName"
android:layout_height="wrap_content"
android:textColor="#897668"
android:layout_weight="1.50"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="0dp"/>
<TextView
android:id="@ id/pmail"
android:layout_height="wrap_content"
android:textColor="#897668"
android:layout_below="@ id/name"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity"
android:background="@color/white">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/playerListRecyclerView"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
Welcome to the Stack Overflow,
your implementation is correct but I guess you forgot to add the Layout Manager for RecyclerView.
so you can do this by adding it in xml or in your java class as :
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity"
android:background="@color/white">
<!-- for Linear layout...-->
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/playerListRecyclerView"
tools:listitem="@layout/playerlist"
tools:itemCount="7"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Or,
you can add it in your java class as
private void featuredRecycler() {
featuredRecycler.setHasFixedSize(true);
ArrayList<plaerListHelperClass> featuredLocation = new ArrayList<>();
featuredLocation.add(new plaerListHelperClass("Shubham","[email protected]"));
featuredLocation.add(new plaerListHelperClass("Shubham","[email protected]"));
featuredLocation.add(new plaerListHelperClass("Shubham","[email protected]"));
featuredLocation.add(new plaerListHelperClass("Shubham","[email protected]"));
adapter=new PlayerListAdapter(featuredLocation);
featuredRecycler.setAdapter(adapter);
featuredRecycler.setLayoutManager(new LinearLayoutManager(this@MainActivity))
}
for more reference you can refer https://www.javatpoint.com/android-recyclerview-list-example
