When I use a recycler view and adapter, the adapter doesn't let the app to install and it causes a crash. I commented out the adapter in my main activity and it started successfully but with adapter it doesn't work.
My adapter class is here:
public class AdapterFree extends RecyclerView.Adapter<AdapterFree.ViewHolder> {
Context context;
List<ModelFree> modelFrees;
public AdapterFree(Context context, List<ModelFree> modelFrees) {
this.context = context;
this.modelFrees = modelFrees;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_free, parent, false);
return new AdapterFree.ViewHolder(view);
//return new ViewHolder(view);
//return new AdapterFree(context, modelFrees).new ViewHolder(view);
/*ViewHolder vh = new ViewHolder(view);
return vh;*/
//here I tried a few codes but didn't help
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ModelFree free = modelFrees.get(position);
DecimalFormat decimalFormat = new DecimalFormat("###,###");
String price = decimalFormat.format(Integer.valueOf(free.getPrice()));
holder.textPriceFree.setText(price + " " + "$");
holder.textVisitFree.setText(free.getVisit());
holder.textTitle.setText(free.getTitle());
holder.textFreePrice.setText(free.getFree());
//holder.imageFree.setImageResource(free.getImage());
holder.imageFree.setImageResource(Integer.parseInt(free.getImage()));
}
@Override
public int getItemCount() {
return modelFrees.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
CardView cardViewFree;
ImageView imageFree;
TextView textTitle, textVisitFree, textPriceFree, textFreePrice;
Typeface typeface = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/vaziri.ttf");
public ViewHolder(@NonNull View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.linearLayout);
cardViewFree = itemView.findViewById(R.id.cardViewFree);
imageFree = itemView.findViewById(R.id.imageFree);
textTitle = itemView.findViewById(R.id.textTitle);
textTitle.setTypeface(typeface);
textVisitFree = itemView.findViewById(R.id.textVisitFree);
textVisitFree.setTypeface(typeface);
textPriceFree = itemView.findViewById(R.id.textPriceFree);
textPriceFree.setTypeface(typeface);
textFreePrice = itemView.findViewById(R.id.textFreePrice);
textFreePrice.setTypeface(typeface);
}
}
}
my main activity :
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
ImageView image1;
CoordinatorLayout coordinator;
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar mainToolbar;
RecyclerView recyclerFree, recyclerOnly, recyclerVisit, recyclerSales;
AdapterFree adapterFree;
List<ModelFree> modelFreeList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinator = findViewById(R.id.coordinator);
drawerLayout = findViewById(R.id.drawerLayout);
navigationView = findViewById(R.id.navigationView);
mainToolbar = findViewById(R.id.main_toolbar);
image1 = findViewById(R.id.image1);
recyclerFree = findViewById(R.id.recyclerFree);
recyclerOnly = findViewById(R.id.recyclerOnly);
recyclerVisit = findViewById(R.id.recyclerVisit);
recyclerSales = findViewById(R.id.recyclerSales);
setSupportActionBar(mainToolbar);
ActionBarDrawerToggle toggle;
toggle = new ActionBarDrawerToggle(this, drawerLayout, mainToolbar, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
Picasso.get().load(R.drawable.lexuse).into(image1);
setDataFree();
}
private void setDataFree() {
adapterFree = new AdapterFree(getApplicationContext(), modelFreeList);
recyclerFree.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
recyclerFree.setAdapter(adapterFree);
modelFreeList.add(new ModelFree(1, String.valueOf(R.drawable.lexus2), "lexus2", "10", "10000$", "5$"));
modelFreeList.add(new ModelFree(2, String.valueOf(R.drawable.lexus3), "lexus3", "20", "20000$", "6$"));
modelFreeList.add(new ModelFree(3, String.valueOf(R.drawable.lexus3), "lexus3", "30", "30000$", "7$"));
modelFreeList.add(new ModelFree(4, String.valueOf(R.drawable.lexus4), "lexus4", "40", "40000$", "8$"));
modelFreeList.add(new ModelFree(5, String.valueOf(R.drawable.lexus5), "lexus5", "50", "50000$", "9$"));
modelFreeList.add(new ModelFree(6, String.valueOf(R.drawable.lexusd), "lexus6", "60", "60000$", "5$"));
adapterFree.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.option_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int optionId = item.getItemId();
switch (optionId) {
case R.id.page2:
startActivity(new Intent(this, MainActivity2.class));
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
my model class :
public class ModelFree {
int id;
String image;
String title;
String visit;
String price;
String free;
public ModelFree(int id, String image, String title, String visit, String price, String free) {
this.id = id;
this.image = image;
this.title = title;
this.visit = visit;
this.price = price;
this.free = free;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getVisit() {
return visit;
}
public void setVisit(String visit) {
this.visit = visit;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getFree() {
return free;
}
public void setFree(String free) {
this.free = free;
}
}
xml layout that I write recycler view:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/purple_200"
android:id="@+id/main_toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/image1"
android:scaleType="fitXY"/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#e7e7e7">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="60dp"
app:cardCornerRadius="10dp"
app:cardElevation="1dp"
app:cardBackgroundColor="@color/teal_200"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:id="@+id/cardCategory">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:layoutDirection="rtl">
<ImageView
android:layout_width="34dp"
android:layout_height="34dp"
android:src="@drawable/ic_baseline_format_list_bulleted_24"
app:tint="@color/cardview_light_background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="18sp"
android:textColor="@color/white"
android:text="Category"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16sp"
android:gravity="left"
android:text="Special Discounts"
android:textColor="@color/black"
android:layout_marginLeft="15dp"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerFree"
android:layoutDirection="ltr"
android:layout_marginTop="10dp"
android:layout_marginLeft="3dp"/>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16sp"
android:gravity="left"
android:text="Just Here"
android:textColor="@color/black"
android:layout_marginLeft="15dp"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerOnly"
android:layoutDirection="ltr"
android:layout_marginTop="10dp"
android:layout_marginLeft="3dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@drawable/lexusa"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="15dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:layout_margin="1dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/lexusb"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:layout_margin="1dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/lexusc"/>
</LinearLayout>
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/black"
android:gravity="left"
android:text="Most visited"
android:layout_marginLeft="15dp"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:layout_marginTop="10dp"
android:layout_marginLeft="3dp"
android:id="@+id/recyclerVisit"/>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/black"
android:gravity="left"
android:text="Most sales"
android:layout_marginLeft="15dp"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:layout_marginTop="10dp"
android:layout_marginLeft="3dp"
android:id="@+id/recyclerSales"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
my xml layout for items:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="170dp"
android:layout_height="260dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:id="@+id/cardViewFree"
android:layout_margin="4dp"
android:orientation="vertical"
app:cardCornerRadius="2dp"
app:cardElevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:visibility="visible"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="170dp"
android:scaleType="centerInside"
android:id="@+id/imageFree"
android:src="@drawable/lexus2"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:id="@+id/textTitle"
android:layout_margin="3dp"
android:gravity="left"
android:text="lexus2"
android:textColor="@color/black"
android:textSize="12dp"
android:singleLine="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="center_vertical"
android:layoutDirection="ltr"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:id="@+id/textVisitFree"
android:text="10"
android:textColor="@color/black"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:src="@drawable/ic_baseline_visibility_24"
app:tint="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:gravity="center_vertical|right"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textPriceFree"
android:gravity="center_vertical|right"
android:text="123000$"
android:textColor="@color/black"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textFreePrice"
android:padding="1dp"
android:layout_marginRight="5dp"
android:gravity="right|center_vertical"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>