0

私はAndroidのアーキテクチャコンポーネントを実装して、バイヤーのリストを表示して選択します。livedata recyclerview with checkbox

は、ここに私のバイヤーエンティティ

@Entity 
data class Buyer(@PrimaryKey var id: Long = 0, var name: String = "", var photo: String = "", var address: String = "", 
       @Ignore var isSelected: Boolean = false, 
       @SerializedName("last_update_time") var lastUpdateTime: Long = 0L) { 

} 

私が挿入され、recyclerviewでそれを表示しています。

私が欲しいのは、あるバイヤーをクリックすると、どのように特定のバイヤーを表示できるかを知ることです。

1人のバイヤーをクリックすると、以前に選択したバイヤーを選択解除する必要があります。

これを実装するのを手伝ってください。私はrecyclerviewにデータを追加する方法ここで

EDIT

class BuyerAdapter(private var buyers: ArrayList<Buyer>, private val listener: View.OnClickListener) : RecyclerView.Adapter<BuyerViewHolder>() { 


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BuyerViewHolder { 
     val v = LayoutInflater.from(parent.context).inflate(
       R.layout.item_buyer, parent, false) 
     return BuyerViewHolder(v) 
    } 

    override fun onBindViewHolder(holder: BuyerViewHolder, position: Int) { 
     bindView(holder, position) 
    } 

    private fun bindView(holder: BuyerViewHolder, position: Int) { 
     val buyer = buyers[position] 
     holder.setName(buyer.name) 
     holder.setAddress(buyer.address) 
     holder.loadImage(ServiceHandler.BASE_URL + buyer.photo) 
     if (buyer.isSelected) { 
      holder.setCardColor(R.color.waveBlue) 
      holder.setNameColor(R.color.white) 
      holder.setAddressColor(R.color.white) 
     } else { 
      holder.setCardColor(R.color.white) 
      holder.setNameColor(R.color.contentGrey) 
      holder.setAddressColor(R.color.contentGreyDesc) 
     } 
     holder.itemView.tag = buyer 
     holder.itemView.setOnClickListener(listener) 
    } 

    override fun getItemCount(): Int = buyers.size 
    fun refresh(newBuyers: ArrayList<Buyer>) { 
     this.buyers = newBuyers 
     notifyDataSetChanged() 
    } 
} 

そして、ここに私のアダプタのxmlアイテムです

<?xml version="1.0" encoding="utf-8"?><!--<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"--><!--xmlns:card_view="http://schemas.android.com/apk/res-auto"--><!--android:layout_width="match_parent"--><!--android:layout_height="191dp"--><!--android:paddingTop="13dp"--><!--&gt;--> 

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/buyer_card" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginTop="4dp" 
    android:clickable="true" 
    card_view:cardCornerRadius="2dp" 
    card_view:cardElevation="4dp" 
    card_view:cardPreventCornerOverlap="true"> 

    <android.support.constraint.ConstraintLayout 
     android:id="@+id/rlBuyerBack" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="4dp"> 
     ​ 

     <de.hdodenhof.circleimageview.CircleImageView 
      android:id="@+id/ivLogo" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:scaleType="centerCrop" 
      android:src="@drawable/placeholder_profile_photo" 
      card_view:layout_constraintBottom_toBottomOf="parent" 
      card_view:layout_constraintLeft_toLeftOf="parent" 
      card_view:layout_constraintTop_toTopOf="parent" /> 

     <in.motiontech.wave.helper.WaveTextView 
      android:id="@+id/tvName" 
      style="@style/semiBoldFont" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:text="Name" 
      android:textColor="@color/contentGrey" 
      android:textSize="@dimen/tSizeHeader" 
      card_view:layout_constraintLeft_toRightOf="@+id/ivLogo" 
      card_view:layout_constraintTop_toTopOf="@+id/ivLogo" /> 


     <in.motiontech.wave.helper.WaveTextView 
      android:id="@+id/tvAddress" 
      style="@style/regularFont" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@id/tvName" 
      android:layout_below="@+id/tvName" 
      android:layout_marginLeft="8dp" 
      android:layout_marginTop="2dp" 
      android:text="Address" 
      android:textColor="@color/contentGrey" 
      android:textSize="@dimen/tSizeDesc" 
      card_view:layout_constraintLeft_toRightOf="@+id/ivLogo" 
      card_view:layout_constraintTop_toBottomOf="@+id/tvName" /> 
    </android.support.constraint.ConstraintLayout> 

</android.support.v7.widget.CardView> 

編集2

viewModel.getBuyers().observe(this, Observer<List<Buyer>> { 
      if (it != null) { 
       if (it.isEmpty()) { 
        showProgress() 
        if (CommonUtils.isInNetwork(this)) { 
         viewModel.getBuyerList() 
        } else { 
         CommonUtils.showNoInternetDialog(this) 
        } 
       } else { 
        hideProgress() 
        buyerAdapter?.refresh(ArrayList(it)) 
       } 
      } 
     }) 
+0

を使用していますように私は、notifyupdateする必要はありません注意してください。 –

+0

私の編集 –

+0

をチェックし、forループを入れ、他の項目を選択解除して特定の項目を選択してから、 'notifyDatasetChanged()'を呼び出してください。単純だが効果的 – Godwin

答えて

0

義和私がしたことは、私はlivedataの価値を更新したことです。あなたは、コードの下に見ることができます:

fun selectBuyer(buyer: Buyer?) { 
     if (buyer == null) 
      return 
     buyers.value?.filter { it != buyer }?.forEach { it.isSelected = false } 
     buyers.value?.get(buyers.value!!.indexOf(buyer))?.isSelected = true 
     newBuyer = buyer 
    } 

私は私はあなたがアダプタの項目のためのアダプター&XMLを投稿すべきだと思うオブザーバーパターン