Below are my ShelflifeAdapter
and HomeFragment
classes:-
public class HomeFragment extends Fragment {
RecyclerView shelflifeRecyclerview;
ShelflifeAdapter shelflifeAdapter;
Product product;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_home, container, false);
shelflifeRecyclerview = rootView.findViewById(R.id.shelflifeRecyclerview);
shelflifeAdapter = new ShelflifeAdapter(getContext());
shelflifeRecyclerview.setAdapter(shelflifeAdapter);
shelflifeRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
shelflifeRecyclerview.setHasFixedSize(true);
//fragment->fragment 데이터받기
Bundle bundle = getArguments(); //번들 받기
if(bundle != null){
product = new Product();
product = (Product) bundle.getSerializable("bundle");
shelflifeAdapter.addItem(product);
Log.d("TAG", shelflifeAdapter.getItemCount() +" 갯수");
}
return rootView;
}
}
public class ShelflifeAdapter extends RecyclerView.Adapter<ShelflifeAdapter.ViewHolder>{
ArrayList<Product> items = new ArrayList<Product>();
Context mContext;
ShelflifeAdapter(Context context){
mContext = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Log.d("TAG","onCreateViewHolder 작동함");
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.layout_shelflife,parent,false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
Log.d("TAG","onBindViewHolder 작동함");
Product item = items.get(position);
viewHolder.setItem(item,mContext);
}
@Override
public int getItemCount() {
return items.size();
}
public void addItem(Product item){
if(item != null){
Log.d("TAG","addItem 작동함(item 추가) : " + item.toString());
items.add(item);
notifyDataSetChanged();
}else{
Log.d("TAG","addItem 작동함(item 못가져옴)");
}
}
public void setItems(ArrayList<Product> items){
this.items = items;
}
public Product getItem(int position){
return items.get(position);
}
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView tv_shelflife;
LinearLayout itemContainer;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
tv_shelflife = itemView.findViewById(R.id.tv_shelflife);
itemContainer = itemView.findViewById(R.id.itemContainer);
}
public void setItem(Product item, Context context){
if(item != null){
Log.d("TAG","어댑터의 item : "+ item.toString());
if(item.getImage()==null){
Glide.with(itemView).load(R.drawable.no_image).override(48,48).into(imageView);
}else{
Glide.with(itemView).load(item.image).override(48,48).into(imageView);
}
tv_shelflife.setText(item.shelflifeDate);
}else{
Log.d("TAG","item이 null임" );
}
}
}
}
addItem(Product item)
is working well But onCreateViewHolder
method is not working. Any help is appreciated !!!
initialize recycler view first,then set adapter for recycler view.
shelflifeAdapter = new ShelflifeAdapter(getContext());
shelflifeRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
shelflifeRecyclerview.setHasFixedSize(true);
//Always set adapter after setLayoutManager, setHasFixedSize
shelflifeRecyclerview.setAdapter(shelflifeAdapter);
Bundle bundle = getArguments(); //번들 받기
if(bundle != null){
product = new Product();
product = (Product) bundle.getSerializable("bundle");
shelflifeAdapter.addItem(product);
Log.d("TAG", shelflifeAdapter.getItemCount() +" 갯수");
}
And make sure item is not null
Product item = items.get(position);
if(item!=null)
viewHolder.setItem(item,mContext);
pass context into glide
if(item.getImage()==null){
Glide.with(context).load(R.drawable.no_image).override(48,48).into(imageView);
}else{
Glide.with(context).load(item.image).override(48,48).into(imageView);
}
tv_shelflife.setText(item.shelflifeDate);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.