lundi 11 septembre 2017

add action in RecyclerView

i am have RecyclerView and CardView

CardView have CheckBox

i want check if CheckBox check or no in each item in RecyclerView

and i want delete item if CheckBox not checked

any method help me ?

this my xml :

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rec"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


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

and this my adapter class :

  class Adapter( private val cartlist: ArrayList<card>,val context: Context): RecyclerView.Adapter<Adapter.MyViewHolder>() {


override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): MyViewHolder {

    val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.tkt_all_station, viewGroup, false)
    return MyViewHolder(view)
}


override fun onBindViewHolder(holder: MyViewHolder, position: Int) {


    val cart:card = cartlist[position]

    holder.title.text = cart.station
    holder.msg.text = cart.x.toString()
    holder.date.text = cart.y.toString()


}


override fun getItemCount(): Int {

    return cartlist.size

}


class MyViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView) {

    var title = itemView.findViewById<TextView>(R.id.stationNmae) as TextView
    var msg = itemView.findViewById<TextView>(R.id.xall) as TextView
    var date = itemView.findViewById<TextView>(R.id.yall) as TextView



}}

and this my main activity :

 class All_Station : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_all__station)
    rec.layoutManager= LinearLayoutManager(this, LinearLayout.VERTICAL,false)
    var db = FirebaseDatabase.getInstance().getReference()
    var listOfStation = ArrayList<card>()



    db.child("latitude and longitude").addValueEventListener(object :ValueEventListener{


        override fun onDataChange(p0: DataSnapshot?) {


            var nour = p0!!.value as HashMap<String,Any>



for (key in nour.keys){
 var data = nour[key]as HashMap<String,Any>
 listOfStation.add(card(

         data["jop"] as String, data["latitude"] as String, data["longitude"] as String
 ))
            }
            var adapter = Adapter(listOfStation ,this@All_Station )
           rec.adapter = adapter
      }

        override fun onCancelled(p0: DatabaseError?) {

        }


    })
}

...............................................................................




Aucun commentaire:

Enregistrer un commentaire