This commit is contained in:
2026-03-17 10:35:41 +00:00
parent 948921a424
commit 88ef4b6796
8 changed files with 36 additions and 116 deletions

View File

@@ -3,25 +3,20 @@ package com.example.pap_teste;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.pap_teste.models.Restaurant;
import java.util.List;
public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.ViewHolder> {
private List<Restaurant> restaurants;
private OnRestaurantClickListener listener;
public interface OnRestaurantClickListener {
void onRestaurantClick(Restaurant restaurant);
}
private final List<Restaurant> restaurants;
private final OnRestaurantClickListener listener;
public RestaurantAdapter(List<Restaurant> restaurants, OnRestaurantClickListener listener) {
this.restaurants = restaurants;
this.listener = listener;
@@ -30,23 +25,16 @@ public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.Vi
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_restaurant, parent, false);
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Restaurant restaurant = restaurants.get(position);
holder.txtName.setText(restaurant.getName());
holder.txtCategory.setText(restaurant.getCategory());
updateFavoriteIcon(holder.btnFavorite, restaurant.isFavorite());
holder.btnFavorite.setOnClickListener(v -> {
restaurant.setFavorite(!restaurant.isFavorite());
updateFavoriteIcon(holder.btnFavorite, restaurant.isFavorite());
});
holder.text1.setText(restaurant.getName());
holder.text2.setText(restaurant.getCategory() + (restaurant.isAvailable() ? " - Disponível" : " - Indisponível"));
holder.itemView.setOnClickListener(v -> {
if (listener != null) {
listener.onRestaurantClick(restaurant);
@@ -54,28 +42,17 @@ public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.Vi
});
}
private void updateFavoriteIcon(ImageButton btn, boolean isFavorite) {
if (isFavorite) {
btn.setImageResource(android.R.drawable.btn_star_big_on);
} else {
btn.setImageResource(android.R.drawable.btn_star_big_off);
}
}
@Override
public int getItemCount() {
return restaurants.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView txtName, txtCategory;
ImageButton btnFavorite;
TextView text1, text2;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtName = itemView.findViewById(R.id.txtRestaurantName);
txtCategory = itemView.findViewById(R.id.txtRestaurantCategory);
btnFavorite = itemView.findViewById(R.id.btnFavorite);
text1 = itemView.findViewById(android.R.id.text1);
text2 = itemView.findViewById(android.R.id.text2);
}
}
}