Skip to content

Instantly share code, notes, and snippets.

@MwBakker
Last active October 31, 2018 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MwBakker/ee5c81c933e7989024622ea601240860 to your computer and use it in GitHub Desktop.
Save MwBakker/ee5c81c933e7989024622ea601240860 to your computer and use it in GitHub Desktop.
public class StorageListAdapter extends RecyclerView.Adapter<StorageListAdapter.StorageViewHolder> {
static RecyclerviewClickListener _listener;
//
// internal class ViewHolder
//
static class StorageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
// the repeating item in the adapter
final Button storageItemView;
Context _context;
// constructor setting the clicklisteners and buttons
StorageViewHolder(View itemView, Context context, RecyclerviewClickListener listener)
{
super(itemView);
_listener = listener;
_context = context;
storageItemView = itemView.findViewById(R.id.storageButton);
itemView.setOnClickListener(this);
//itemView.setOnLongClickListener(this);
}
public void setOnItemClickListener(RecyclerviewClickListener clickListener)
{
StorageListAdapter._listener = clickListener;
}
@Override
public void onClick(View v)
{
Intent intent = new Intent(_context, StorageActivity.class);
intent.putExtra("storageID", ((Button) v).getId());
_context.startActivity(intent);
}
public boolean onLongClick(View v)
{
_listener.onItemLongClick(getAdapterPosition(), v);
return false;
}
}
//
// _________________________________________________
//
final LayoutInflater _inflater;
List<Storage> _storages;
Context _context;
// constructor, recieving the context
public StorageListAdapter(Context context)
{
_context = context;
_inflater = LayoutInflater.from(context);
}
// viewholder creator, returner with listener and viewItem attached
@Override
public StorageViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = _inflater.inflate(R.layout.button_storage, parent, false);
return new StorageViewHolder(itemView, _context, _listener);
}
// binds the viewholder to the adapter
@Override
public void onBindViewHolder(StorageViewHolder holder, int position)
{
if (_storages != null)
{
Storage current = _storages.get(position);
String buttonTitle = current.get_storageName() + "\n \uD83D\uDCCD " + current.get_storageLoc();
holder.storageItemView.setText(buttonTitle);
holder.storageItemView.setId(current._storageID);
}
else
{
// covers the case of data not being ready yet.
holder.storageItemView.setText("No Word");
}
}
// getItemCount() is called many times, and when it is first called,
// storage has not been updated (means initially, it's null, and we can't return null).
@Override
public int getItemCount()
{
if (_storages != null)
{
return _storages.size();
}
else
{
return 0;
}
}
// sets the storages
public void setStorages(List<Storage> storages)
{
_storages = storages;
notifyDataSetChanged();
}
}
package com.mwb.digitalstorage.adapter;
public class ShelfListAdapter extends RecyclerView.Adapter<ShelfListAdapter.ShelfViewHolder> {
static RecyclerviewClickListener _listener;
//
// internal class ViewHolder
//
public static class ShelfViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
// the repeating item in the adapter
final Button shelfItemView;
Context _context;
// constructor setting the clicklisteners and buttons
ShelfViewHolder(View itemView, Context context, RecyclerviewClickListener listener)
{
super(itemView);
_listener = listener;
_context = context;
shelfItemView = itemView.findViewById(R.id.storageButton);
itemView.setOnClickListener(this);
//itemView.setOnLongClickListener(this);
}
public void setOnItemClickListener(RecyclerviewClickListener clickListener)
{
ShelfListAdapter._listener = clickListener;
}
@Override
public void onClick(View v)
{
// Intent intent = new Intent(_context, StorageActivity.class);
// intent.putExtra("shelfID", ((Button) v).getText().toString());
// _context.startActivity(intent);
}
public boolean onLongClick(View v)
{
_listener.onItemLongClick(getAdapterPosition(), v);
return false;
}
}
//
// _________________________________________________
//
final LayoutInflater _inflater;
List<Shelf> _shelfs;
Context _context;
// constructor, recieving the context
public ShelfListAdapter(Context context)
{
_context = context;
_inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ShelfViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View itemView = _inflater.inflate(R.layout.button_storage, parent, false);
return new ShelfViewHolder(itemView, _context, _listener);
}
@Override
public void onBindViewHolder(@NonNull ShelfViewHolder holder, int position)
{
if (_shelfs != null) {
Shelf current = _shelfs.get(position);
holder.shelfItemView.setText(current.get_shelfName() + "\n \uD83D\uDCCD " + current.get_shelfLoc());
} else {
// covers the case of data not being ready yet.
holder.shelfItemView.setText("No shelfs");
}
}
@Override
public int getItemCount()
{
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment