I am trying to display data in my recycler view in java. But I am not able to display data in my recycler view. Where am I wrong. I have no idea where I have gone wrong. Please help. This is my adapter class.
public class ListAdapter1 extends RecyclerView.Adapter<ListAdapter1.ViewHolder> {
Context context;
List<SupermarketModels> supermarketModels;
public ListAdapter1(Context context, List<SupermarketModels> supermarketModels) {
this.context = context;
this.supermarketModels = supermarketModels;
}
@NonNull
@org.jetbrains.annotations.NotNull
@Override
public ListAdapter1.ViewHolder onCreateViewHolder(@NonNull @org.jetbrains.annotations.NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.tileslist,null);
return new ListAdapter1.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull @org.jetbrains.annotations.NotNull ListAdapter1.ViewHolder holder, int position) {
holder.tvvegetables.setText(supermarketModels.get(position).getVegetables());
holder.tvquantity.setText(supermarketModels.get(position).getQuantity());
holder.tvprice.setText(supermarketModels.get(position).getPrice());
holder.tvexpiry.setText(supermarketModels.get(position).getExpiry());
}
@Override
public int getItemCount() {
return supermarketModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvvegetables, tvquantity, tvprice, tvexpiry;
public ViewHolder(@NonNull @NotNull View itemView) {
super(itemView);
tvvegetables = itemView.findViewById(R.id.vegetables);
tvquantity = itemView.findViewById(R.id.quantity);
tvprice = itemView.findViewById(R.id.price);
tvexpiry = itemView.findViewById(R.id.expiry);
}
}
}
This is my main activity class. I am trying to display a sample data in the recycler inside the main activity. Here I am displaying data from model class namely SupermarketModels
.
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ListAdapter1 listAdapter;
List<SupermarketModels> supermarketModelsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
supermarketModelsList.add(new SupermarketModels(1,"sugar cane","20kg",
"50rs","31/12/2021"));
supermarketModelsList.add(new SupermarketModels(2,"sugar cane","20kg",
"50rs","31/12/2021"));
supermarketModelsList.add(new SupermarketModels(3 ,"sugar cane","20kg",
"50rs","31/12/2021"));
initialization();
setadapter(supermarketModelsList);
}
private void initialization(){
recyclerView = findViewById(R.id.recyclerview);
}
private void setadapter(List<SupermarketModels> supermarketModels){
listAdapter = new ListAdapter1(this, supermarketModels);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(listAdapter);
}
}
This is my xml code.
<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=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@ id/maintoolbar"
android:background="@color/white"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@ id/maintoolbar"
android:orientation="vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@ id/recyclerview"
/>
</LinearLayout>
</RelativeLayout>
CodePudding user response:
Do you have any error message on logcat? or check your xml layout. It looks like there is no problem with your code
CodePudding user response:
I have checked your code and its displayed items , issue is in your main xml layout . Where you are adding toolbar , do wrap content
of your toolbar layout height then it will display items. like this
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/maintoolbar"
android:background="@color/white"
/>
CodePudding user response:
You shuold set layoutmanager on your recyclerview:
recyclerView.setLayoutManager(new LinearLayoutManager(context))
The LinearLayoutManager
is a vertical/horizontal LayoutManager. You can use GridLayoutManager
if you want to show data with grid layout