我正在尝试将来 self 服务器的数据放入我的应用程序中, 使用 RecyclerView 和 volley,现在因为我正在使用一个适配器,这是我的适配器类
class TypeAdapter(var con: Context, var list: ArrayList<TypeItems>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
(p0 as ItemView).bind(list[p1].cartype, list[p1].typetype, list[p1].modeltype, list[p1].photype)
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
val v= LayoutInflater.from(con).inflate(R.layout.car_model_item, p0, false)
return ItemView(v)
}
override fun getItemCount(): Int {
return list.size
}
class ItemView(itemVeiw: View) : RecyclerView.ViewHolder(itemVeiw) {
fun bind(car_type: String, type: String, modele: String, ph: String) {
Picasso.with(itemView.context).load(ph).into(itemView.type)
itemView.name.text= "$car_type $type"
itemView.model.text= modele
}
}
}
这是我的 TypeItems 类
class TypeItems(car_typetype: String, typetype: String, modeletype: String, phtype: String) {
var cartype:String = car_typetype
var typetype:String = typetype
var modeltype:String = modeletype
var photype:String = phtype
}
这是我的主课
class CarModelActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_car_model)
val list= ArrayList<TypeItems>()
val rq= Volley.newRequestQueue(this)
val url= ("http://test123456789.cf/sell_items/reportall.php?car_type=${car_item.cartype}").replace(" ", "%20")
val srr= StringRequest(Request.Method.GET, url ,
Response.Listener { response ->
//converting the string to json array object
val array = JSONArray(response)
list.clear()
//traversing through all the object
for (i in 0 until array.length()) {
//getting item object from json array
val product = array.getJSONObject(i)
//ex Image
var url2= "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRj3LA1ERJCx8lM-qHsgaW_5IgzeW21w-Ona7iI05E9aoXrImtl"
//adding the product to item list
list.add(
TypeItems(
product.getString("car_type"),
product.getString("type"),
product.getString("model"),
url2
)
)
}
//creating adapter object and setting it to recyclerview
val adapter = TypeAdapter(this.applicationContext, list)
rv.layoutManager= GridLayoutManager(this, 2)
rv.adapter = adapter
}, Response.ErrorListener { error ->
Toast.makeText(this, error.message, Toast.LENGTH_LONG).show()
})
rq.add(srr)
}
}
现在我想从我的列表中删除重复的项目,我想按 car_type 和 type 和 model 排序,如果该项目重复,我想删除它
最佳答案
您应该使用数据类。它将在内部包含重写的 equals()
方法:
data class TypeItems(
val car_typetype: String,
val typetype: String,
val modeletype: String,
val phtype: String
)
深入研究这个问题后,我发现,您不能在 Set
集合上调用方法 get()
。所以,这段代码将不起作用:
(p0 作为 ItemView).bind(list[p1].cartype, list[p1].typetype, list[p1].modeltype, list[p1].photype)
总结一下,Set
帮不了你。要解决您的问题,您只需要调用防御检查:
val typeItems = TypeItems(
product.getString("car_type"),
product.getString("type"),
product.getString("model"),
url2
)
if(!list.contains(typeItems)) {
list.add(typeItems)
}
因此,还有另一种方法可以解决这个问题:
代替
val adapter = TypeAdapter(this.applicationContext, list)
调用
val adapter = TypeAdapter(this.applicationContext, list.distinct())
方法 distinct()
以相同的顺序返回列表的唯一值。不要忘记让它成为数据类。
关于java - 如何删除类的arraylist中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57465779/