Created
November 28, 2018 16:24
-
-
Save MwBakker/e57adc4a19a2d8c59ca0120c93d7122c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | |
<data> | |
<variable | |
name="commandHandlers" | |
type="com.mwb.digitalstorage.command_handlers.ItemHandler"/> | |
<variable | |
name="storage" | |
type="com.mwb.digitalstorage.model.Storage"/> | |
</data> | |
<LinearLayout | |
style="@style/match_parent"> | |
<Button | |
style="@style/btn_storage_list" | |
android:layout_marginLeft="50dp" | |
android:layout_marginRight="50dp" | |
android:text="@{storage.storageName}" | |
android:onClick="@{(view) -> commandHandlers.goToItem(view)}"/> | |
</LinearLayout> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mwb.digitalstorage.adapter; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.mwb.digitalstorage.R; | |
import com.mwb.digitalstorage.StorageActivity; | |
import com.mwb.digitalstorage.command_handlers.ItemHandler; | |
import com.mwb.digitalstorage.databinding.StorageItemBinding; | |
import com.mwb.digitalstorage.model.Storage; | |
import java.util.List; | |
import androidx.annotation.NonNull; | |
import androidx.databinding.DataBindingUtil; | |
import androidx.recyclerview.widget.RecyclerView; | |
public class StorageListAdapter extends RecyclerView.Adapter<StorageListAdapter.StorageViewHolder>{ | |
private List<Storage> storageUnits; | |
private Context context; | |
public StorageListAdapter(List<Storage> storageUnits) | |
{ | |
this.storageUnits = storageUnits; | |
} | |
// | |
// creates the viewHolder | |
// | |
@Override | |
public StorageListAdapter.StorageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) | |
{ | |
this.context = parent.getContext(); | |
LayoutInflater inflater = LayoutInflater.from(context); | |
StorageItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.storage_item, parent, false); | |
return new StorageViewHolder(binding); | |
} | |
// | |
// binds recyclerView to viewHolder | |
// | |
@Override | |
public void onBindViewHolder(@NonNull StorageListAdapter.StorageViewHolder holder, int position) | |
{ | |
Storage storage = storageUnits.get(position); | |
holder.bind(storage); | |
} | |
// | |
// returns the list size | |
// | |
@Override | |
public int getItemCount() | |
{ | |
if (storageUnits != null) { return storageUnits.size(); } | |
else { return 0; } | |
} | |
//__________________________________________________________________________________________________ | |
// | |
// internal class ViewHolder | |
// | |
class StorageViewHolder extends RecyclerView.ViewHolder | |
{ | |
private StorageItemBinding binding; | |
StorageViewHolder(StorageItemBinding binding) | |
{ | |
super(binding.getRoot()); | |
this.binding = binding; | |
setHandlers(); | |
} | |
// | |
// sets handlers belonging to the holder | |
// | |
private void setHandlers() | |
{ | |
// handlers and methods | |
binding.setCommandHandlers(new ItemHandler() | |
{ | |
@Override | |
public void goToItem(View view) | |
{ | |
context.startActivity(createIntent(view)); | |
} | |
}); | |
} | |
// | |
// create intent | |
// | |
private Intent createIntent(View v) | |
{ | |
Intent intent = new Intent(context, StorageActivity.class); | |
int storageID = v.getId(); | |
intent.putExtra("storageID", storageID); | |
for (Storage storage : storageUnits) | |
{ | |
if (storage.storageID == storageID) | |
{ | |
intent.putExtra("storage_name", storage.getStorageName()); | |
intent.putExtra("storage_loc", storage.getStorageName()); | |
} | |
} | |
return intent; | |
} | |
// | |
// binds the storage unit to the model | |
// | |
private void bind(@NonNull Storage storage) | |
{ | |
binding.setStorage(storage); | |
binding.executePendingBindings(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment