/* Ċ Ġ* ĠCopyright Ġ( C ) Ġ 2 0 1 7 ĠGoogle ĠInc . Ċ Ġ* Ċ Ġ* ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* ĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ Ċ package Ġcom . example . android . stock room Ċ Ċ import Ġandroid . content . Context Ċ import Ġandroid . graphics . Color Ċ import Ġandroid . util . Typed Value Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroid . widget . LinearLayout Ċ import Ġandroid . widget . TextView Ċ import Ġandroidx . constraint layout . widget . Constraint Layout Ċ import Ġandroidx . recyclerview . widget . RecyclerView Ċ import Ġkotlinx . android . synt hetic . main . asset view _ item . view . textView Asset Items Layout Ċ import Ġkotlinx . android . synt hetic . main . event view _ item . view . textView Event DateTime Ċ import Ġkotlinx . android . synt hetic . main . event view _ item . view . textView Event Delete Ċ import Ġkotlinx . android . synt hetic . main . event view _ item . view . textView Event Items Layout Ċ import Ġkotlinx . android . synt hetic . main . event view _ item . view . textView Event Note Ċ import Ġkotlinx . android . synt hetic . main . event view _ item . view . textView Event Title Ċ import Ġjava . time . LocalDateTime Ċ import Ġjava . time . Zone Offset Ċ import Ġjava . time . format . DateTime Formatter Ċ import Ġjava . time . format . Format Style . MEDIUM Ċ import Ġjava . time . format . Format Style . SHORT Ċ Ċ // Ġhttps :// code labs . developers . google . com / code labs / kotlin - android - training - diff util - databinding /# 4 Ċ Ċ class ĠEvent ListAdapter Ġinternal Ġconstructor ( ĊĠ Ġcontext : ĠContext , ĊĠ Ġprivate Ġval Ġclick Listener Update : Ġ( Event ) Ġ-> ĠUnit , ĊĠ Ġprivate Ġval Ġclick Listener Delete : Ġ( Event ) Ġ-> ĠUnit Ċ ) Ġ: ĠRecyclerView . Adapter < Event ListAdapter . Event ViewHolder >() Ġ{ ĊĊĠ Ġprivate Ġval Ġcontext Ġ= Ġcontext ĊĠ Ġprivate Ġval Ġinflater : ĠLayoutInflater Ġ= ĠLayoutInflater . from ( context ) ĊĠ Ġprivate Ġvar Ġevent List Ġ= Ġmutable ListOf < Event >() ĊĊĠ Ġinner Ġclass ĠEvent ViewHolder ( item View : ĠView ) Ġ: ĠRecyclerView . ViewHolder ( item View ) Ġ{ ĊĠĠĠ Ġfun Ġbind Update ( ĊĠĠĠĠĠ Ġevent : ĠEvent , ĊĠĠĠĠĠ Ġclick Listener Update : Ġ( Event ) Ġ-> ĠUnit ĊĠĠĠ Ġ) Ġ{ ĊĠĠĠĠĠ ĠitemView . textView Event Items Layout . setOnClickListener Ġ{ Ġclick Listener Update ( event ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġbind Delete ( ĊĠĠĠĠĠ Ġevent : ĠEvent , ĊĠĠĠĠĠ Ġclick Listener Delete : Ġ( Event ) Ġ-> ĠUnit ĊĠĠĠ Ġ) Ġ{ ĊĠĠĠĠĠ ĠitemView . textView Event Delete . setOnClickListener Ġ{ Ġclick Listener Delete ( event ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval ĠtextView Event Title : ĠTextView Ġ= ĠitemView . findViewById ( R . id . textView Event Title ) ĊĠĠĠ Ġval ĠtextView Event Note : ĠTextView Ġ= ĠitemView . findViewById ( R . id . textView Event Note ) ĊĠĠĠ Ġval ĠtextView Event DateTime : ĠTextView Ġ= ĠitemView . findViewById ( R . id . textView Event DateTime ) ĊĠĠĠ Ġval ĠtextView Event Delete : ĠTextView Ġ= ĠitemView . findViewById ( R . id . textView Event Delete ) ĊĠĠĠ Ġval ĠtextView Event Layout : ĠConstraint Layout Ġ= ĠitemView . findViewById ( R . id . textView Event Layout ) ĊĠĠĠ Ġval ĠtextView Event Items Layout : ĠLinearLayout Ġ= ĊĠĠĠĠĠ ĠitemView . findViewById ( R . id . textView Event Items Layout ) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun ĠonCreate ViewHolder ( ĊĠĠĠ Ġparent : ĠViewGroup , ĊĠĠĠ Ġview Type : ĠInt ĊĠ Ġ): ĠEvent ViewHolder Ġ{ ĊĠĠĠ Ġval ĠitemView Ġ= Ġinflater . inflate ( R . layout . event view _ item , Ġparent , Ġfalse ) ĊĠĠĠ Ġreturn ĠEvent ViewHolder ( item View ) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon Bind ViewHolder ( ĊĠĠĠ Ġholder : ĠEvent ViewHolder , ĊĠĠĠ Ġposition : ĠInt ĊĠ Ġ) Ġ{ ĊĠĠĠ Ġval Ġcurrent : ĠEvent Ġ= Ġevent List [ position ] ĊĊĠĠĠ Ġ// ĠFirst Ġentry Ġis Ġheadline . ĊĠĠĠ Ġif Ġ( position Ġ== Ġ 0 ) Ġ{ ĊĠĠĠĠĠ Ġholder . textView Event Title . text Ġ= Ġcontext . getString ( R . string . event _ title ) ĊĠĠĠĠĠ Ġholder . textView Event Note . text Ġ= Ġcontext . getString ( R . string . event _ note ) ĊĠĠĠĠĠ Ġholder . textView Event DateTime . text Ġ= Ġcontext . getString ( R . string . event _ datetime ) ĊĠĠĠĠĠ Ġholder . textView Event Delete . visibility Ġ= ĠView . GONE ĊĠĠĠĠĠ Ġholder . textView Event Layout . set BackgroundColor ( context . getColor ( R . color . background List Color )) ĊĊĠĠĠĠĠ Ġval Ġbackground Ġ= ĠTyped Value () ĊĠĠĠĠĠ Ġholder . textView Event Items Layout . setBackground Resource ( background . resourceId ) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠ Ġholder . bind Update ( current , Ġclick Listener Update ) ĊĠĠĠĠĠ Ġholder . bind Delete ( current , Ġclick Listener Delete ) ĊĊĠĠĠĠĠ Ġholder . textView Event Title . text Ġ= Ġcurrent . title ĊĠĠĠĠĠ Ġholder . textView Event Note . text Ġ= Ġcurrent . note ĊĠĠĠĠĠ Ġval Ġdatetime : ĠLocalDateTime Ġ= ĠLocalDateTime . of Epoch Second ( current . datetime , Ġ 0 , ĠZone Offset . UTC ) ĊĠĠĠĠĠ Ġholder . textView Event DateTime . text Ġ= ĊĠĠĠĠĠĠĠ Ġ"${ datetime . format ( DateTime Formatter . of Localized Date ( MEDIUM )) ĊĠĠĠĠĠĠĠ Ġ}\ n ${ datetime . format ( DateTime Formatter . of Localized Time ( MEDIUM )) ĊĠĠĠĠĠĠĠ Ġ}" ĊĊĠĠĠĠĠ Ġval Ġbackground Ġ= ĠTyped Value () ĊĠĠĠĠĠ Ġcontext . theme . resolve Attribute ( android . R . attr . selectable Item Background , Ġbackground , Ġtrue ) ĊĠĠĠĠĠ Ġholder . textView Event Items Layout . setBackground Resource ( background . resourceId ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġinternal Ġfun Ġupdate Events ( events : ĠList < Event >) Ġ{ ĊĠĠĠ Ġ// ĠHead line Ġplaceholder ĊĠĠĠ Ġevent List Ġ= Ġmutable ListOf ( Event ( symbol Ġ= Ġ"", Ġtype Ġ= Ġ 0 , Ġtitle Ġ= Ġ"", Ġnote Ġ= Ġ"", Ġdatetime Ġ= Ġ 0 L )) ĊĠĠĠ Ġevent List . addAll ( events ) ĊĊĠĠĠ Ġnotify DataSet Changed () ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun ĠgetItem Count () Ġ= Ġevent List . size Ċ } Ċ