如果同一数组列表中的值仍然计数为 1,如何获取 kotlin 的 arraylist<object> 中的计数值?
how to get count value in arraylist<object> for kotlin, if value in same array list will still count 1?
我想获取 arrayString 的一个子项的计数值。所以我有 Arraylist (),其中包含 {name, room_block, registration number} 让我们说值
- {费萨尔, A-1, 628345}
- {阿贡, A-1, 932983}
- {杰弗里, B-1, 7820179}
然后我想从 Room_Block 中获取计数值,这样它将 return 该值。
房间数=2,即A-1和B-1
this is my temporary code which is false
this is my Arraylist value form
val dao = DAOTahanan()
dao.get().addValueEventListener(object : ValueEventListener {
@SuppressLint("NotifyDataSetChanged")
override fun onDataChange(snapshot: DataSnapshot) {
emps.clear()
for (data in snapshot.children) {
val emp = data.getValue(tahanan::class.java)
emp!!.setkey(data.key)
emps.add(emp)
}
temporay.sortWith { lhs, rhs ->
rhs!!.name?.let {
lhs!!.name!!.compareTo(
it
)
}!!
}
//filter jika nilai ada yang ganda
temp.clear()
for(item in tempora){
if (!temp.stream().anyMatch { t-> t?.blok_kamar == item?.blok_kamar }){
temp.add(item)
}else{
Log.e("Check duplikat value", "__${tempora.stream().count().toInt()}")
}
}
temp.sortWith { lhs, rhs ->
rhs!!.blok_kamar?.let {
lhs!!.blok_kamar!!.compareTo(
it
)
}!!
}
//adapter & viewmodel cardslide
adap = adaptercardslide_levelsdua(context!!, tempora)
rec!!.layoutManager = LinearLayoutManager(context!!, LinearLayoutManager.HORIZONTAL, false)
rec!!.adapter = adap
adap?.setItems(temp)
这是我获取数据的代码 Arraylist()
这是我在适配器回收视图中的代码:
package com.example.testing.model
import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.testing.Dashboard.detail.Ac_detail_tahanan
import com.example.testing.Database.tahanan
import com.example.testing.R
import com.example.testing.adapter.RVAdapter
import java.util.stream.Collectors
import kotlin.collections.ArrayList
import androidx.localbroadcastmanager.content.LocalBroadcastManager
class adaptercardslide(private val context: Context, tempora: ArrayList<tahanan?>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val valueint = tempora
var typeface_medium : Typeface? = null
var typeface_bold: Typeface? = null
var typefacesemi: Typeface? = null
var typefacedefauld: Typeface?= null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view: View = LayoutInflater.from(context).inflate(R.layout.card_item, parent, false)
typeface_medium = ResourcesCompat.getFont(context, R.font.poppins_medium)
typeface_bold = ResourcesCompat.getFont(context, R.font.poppins_bold)
typefacesemi = ResourcesCompat.getFont(context, R.font.poppins_semibold)
typefacedefauld = ResourcesCompat.getFont(context, R.font.poppins)
return modelcardslide(view)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val vh: modelcardslide = holder as modelcardslide
val emp: tahanan = list[position]!!
val count = valueint.count {
emp.blok_kamar?.let { it1 -> it?.blok_kamar?.contains(it1) }!!
}
vh.nameblok.text = emp.blok_kamar
vh.nameblok.typeface = typeface_bold
vh.jumlahorang.text = "$count, Tahanan"
vh.jumlahorang.typeface = typefacesemi
vh.titlejumlah.typeface = typeface_bold
vh.btnclik.setOnClickListener {
val ItemName: String = vh.nameblok.text.toString()
val intent = Intent("blok-selected")
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
intent.putExtra("item", ItemName)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
}
}
override fun getItemCount(): Int {
return list.size
}
var list: ArrayList<tahanan?> = ArrayList<tahanan?>()
fun setItems(emp: ArrayList<tahanan?>) {
list.addAll(emp)
}
}
我找到了答案,只需使用 startswith() 进行过滤
这是我使用的代码:
var temporay = ArrayList<tahanan?>()
repeat(temp.size) {
temporay = (temp.stream()
.filter {
it?.blok_kamar!!.startsWith(emp.blok_kamar!![0])
}
.collect(Collectors.toList()) as ArrayList<tahanan?>)
}
我想获取 arrayString 的一个子项的计数值。所以我有 Arraylist
- {费萨尔, A-1, 628345}
- {阿贡, A-1, 932983}
- {杰弗里, B-1, 7820179}
然后我想从 Room_Block 中获取计数值,这样它将 return 该值。
房间数=2,即A-1和B-1
this is my temporary code which is false
this is my Arraylist value form
val dao = DAOTahanan()
dao.get().addValueEventListener(object : ValueEventListener {
@SuppressLint("NotifyDataSetChanged")
override fun onDataChange(snapshot: DataSnapshot) {
emps.clear()
for (data in snapshot.children) {
val emp = data.getValue(tahanan::class.java)
emp!!.setkey(data.key)
emps.add(emp)
}
temporay.sortWith { lhs, rhs ->
rhs!!.name?.let {
lhs!!.name!!.compareTo(
it
)
}!!
}
//filter jika nilai ada yang ganda
temp.clear()
for(item in tempora){
if (!temp.stream().anyMatch { t-> t?.blok_kamar == item?.blok_kamar }){
temp.add(item)
}else{
Log.e("Check duplikat value", "__${tempora.stream().count().toInt()}")
}
}
temp.sortWith { lhs, rhs ->
rhs!!.blok_kamar?.let {
lhs!!.blok_kamar!!.compareTo(
it
)
}!!
}
//adapter & viewmodel cardslide
adap = adaptercardslide_levelsdua(context!!, tempora)
rec!!.layoutManager = LinearLayoutManager(context!!, LinearLayoutManager.HORIZONTAL, false)
rec!!.adapter = adap
adap?.setItems(temp)
这是我获取数据的代码 Arraylist
package com.example.testing.model
import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.testing.Dashboard.detail.Ac_detail_tahanan
import com.example.testing.Database.tahanan
import com.example.testing.R
import com.example.testing.adapter.RVAdapter
import java.util.stream.Collectors
import kotlin.collections.ArrayList
import androidx.localbroadcastmanager.content.LocalBroadcastManager
class adaptercardslide(private val context: Context, tempora: ArrayList<tahanan?>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val valueint = tempora
var typeface_medium : Typeface? = null
var typeface_bold: Typeface? = null
var typefacesemi: Typeface? = null
var typefacedefauld: Typeface?= null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view: View = LayoutInflater.from(context).inflate(R.layout.card_item, parent, false)
typeface_medium = ResourcesCompat.getFont(context, R.font.poppins_medium)
typeface_bold = ResourcesCompat.getFont(context, R.font.poppins_bold)
typefacesemi = ResourcesCompat.getFont(context, R.font.poppins_semibold)
typefacedefauld = ResourcesCompat.getFont(context, R.font.poppins)
return modelcardslide(view)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val vh: modelcardslide = holder as modelcardslide
val emp: tahanan = list[position]!!
val count = valueint.count {
emp.blok_kamar?.let { it1 -> it?.blok_kamar?.contains(it1) }!!
}
vh.nameblok.text = emp.blok_kamar
vh.nameblok.typeface = typeface_bold
vh.jumlahorang.text = "$count, Tahanan"
vh.jumlahorang.typeface = typefacesemi
vh.titlejumlah.typeface = typeface_bold
vh.btnclik.setOnClickListener {
val ItemName: String = vh.nameblok.text.toString()
val intent = Intent("blok-selected")
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
intent.putExtra("item", ItemName)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
}
}
override fun getItemCount(): Int {
return list.size
}
var list: ArrayList<tahanan?> = ArrayList<tahanan?>()
fun setItems(emp: ArrayList<tahanan?>) {
list.addAll(emp)
}
}
我找到了答案,只需使用 startswith() 进行过滤 这是我使用的代码:
var temporay = ArrayList<tahanan?>()
repeat(temp.size) {
temporay = (temp.stream()
.filter {
it?.blok_kamar!!.startsWith(emp.blok_kamar!![0])
}
.collect(Collectors.toList()) as ArrayList<tahanan?>)
}