回收器视图中的 textchangelistener 在调试模式下工作,但在运行时不起作用,可能是什么原因?
有人可以检查一下并帮助我吗? 我在适配器中有一个 textchange 侦听器,在片段中有一个 api 调用。每当文本发生更改时,都会从 API 获取数据,并替换项目。例如,现在该项目正在运行模式下结转。当我从项目a更改项目b时,仅显示项目a,当我将其更改为项目c时,显示项目b,当我将其更改为项目d时,显示项目c。它在调试模式下工作正常。
我的片段:
class upload_invent : Fragment() {
var modelclass=ModelClass()
lateinit var v:View
private var _binding: FragmentUploadInventBinding? = null
private val binding get() = _binding!!
private val recyclerViewAdapteru: RecyclerViewAdapterU by lazy {
RecyclerViewAdapterU(RecyclerViewAdapter.ob.dataSelected)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
@SuppressLint("WrongConstant")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
v= inflater.inflate(R.layout.fragment_upload_invent,container,false)
_binding= FragmentUploadInventBinding.bind(v)
binding.recyclerViewU.apply {
layoutManager=LinearLayoutManager(activity)
adapter = recyclerViewAdapteru
}
binding.searchButton.setOnClickListener{
v.findNavController().navigate(R.id.action_upload_invent_to_search_invent2)
}
return binding.root
}
fun updateStockDetail(skuCode:String,pos: Int){
var skuDetails:ArrayList<ModelClass> = arrayListOf()
val call: Call<List<ModelClass>>? =
ApiClient.instance?.myApi?.getfromsku(skuCode)!!
call!!.enqueue(object : Callback<List<ModelClass>?> {
override fun onResponse(
call: Call<List<ModelClass>?>,
response: Response<List<ModelClass>?>
) {
if (response.body()!=null){
skuDetails =response.body() as ArrayList<ModelClass>}
val x = RecyclerViewAdapter.ob.dataSelected[pos]
//RecyclerViewAdapter.ob.dataSelected.set(pos,skuDetails[0])
println(RecyclerViewAdapter.ob.dataSelected[pos].item_name)
println(x.sku_code)
recyclerViewAdapteru.setData(skuDetails,pos)
}
override fun onFailure(call: Call<List<ModelClass>?>, t: Throwable) {
}
})
}
}
这是我的适配器:
class RecyclerViewAdapterU (var dataList:ArrayList<ModelClass>): RecyclerView.Adapter<RecyclerViewAdapterU.ViewHolder>() {
var _binding: UploadItemViewBinding? = null
val binding get() = _binding!!
lateinit var v:View
var obj:upload_invent = upload_invent()
fun setData(listModel: ArrayList<ModelClass>,pos:Int) {
dataList[pos]=listModel[0]
notifyItemChanged(pos)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerViewAdapterU.ViewHolder {
v = LayoutInflater.from(parent.context).inflate(R.layout.upload_item_view, parent, false)
_binding = UploadItemViewBinding.bind(v)
return ViewHolder(binding.root)
}
override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
holder.bindItems(dataList[position])
}
override fun getItemCount(): Int {
return dataList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(data: ModelClass) {
binding.apply {
itemquant.text=data.item_quant
uploadItemName.text = data.item_name
uploadMfg.text = data.mfg
skuStock.setText(data.item_stock.toString())
skuCode.setText(data.sku_code)
}
updateStockDetail()
}
fun updateStockDetail(){
binding.skuCode.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
}
override fun afterTextChanged(editable: Editable) {
if (editable.length>=3){
//binding.skuCode.removeTextChangedListener(this)
var pos:Int=adapterPosition
var x= editable.toString().trim()
println("$x in textwatcher and $pos")
obj.updateStockDetail(editable.toString(),pos)
//notifyDataSetChanged()
//dataList[pos]=RecyclerViewAdapter.ob.dataSelected[pos]
notifyItemChanged(pos)
v.findNavController().navigate(R.id.action_upload_invent_refresh)
}}
})
}
}
}
注意:ob.dataselected 来自另一个 recyclerview。
Can someone please check this and help me?
I have a textchange listener in the adapter, and api call in fragment. whenever there is text change, data is fetched from API, and item is replaced. Now the item is being carry forward in run mode, for example. When i change item b from item a, item a is only shown, when i change it to item c, item b is shown, when i change it to item d, item c is shown. It works fine in debug mode.
my fragment:
class upload_invent : Fragment() {
var modelclass=ModelClass()
lateinit var v:View
private var _binding: FragmentUploadInventBinding? = null
private val binding get() = _binding!!
private val recyclerViewAdapteru: RecyclerViewAdapterU by lazy {
RecyclerViewAdapterU(RecyclerViewAdapter.ob.dataSelected)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
@SuppressLint("WrongConstant")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
v= inflater.inflate(R.layout.fragment_upload_invent,container,false)
_binding= FragmentUploadInventBinding.bind(v)
binding.recyclerViewU.apply {
layoutManager=LinearLayoutManager(activity)
adapter = recyclerViewAdapteru
}
binding.searchButton.setOnClickListener{
v.findNavController().navigate(R.id.action_upload_invent_to_search_invent2)
}
return binding.root
}
fun updateStockDetail(skuCode:String,pos: Int){
var skuDetails:ArrayList<ModelClass> = arrayListOf()
val call: Call<List<ModelClass>>? =
ApiClient.instance?.myApi?.getfromsku(skuCode)!!
call!!.enqueue(object : Callback<List<ModelClass>?> {
override fun onResponse(
call: Call<List<ModelClass>?>,
response: Response<List<ModelClass>?>
) {
if (response.body()!=null){
skuDetails =response.body() as ArrayList<ModelClass>}
val x = RecyclerViewAdapter.ob.dataSelected[pos]
//RecyclerViewAdapter.ob.dataSelected.set(pos,skuDetails[0])
println(RecyclerViewAdapter.ob.dataSelected[pos].item_name)
println(x.sku_code)
recyclerViewAdapteru.setData(skuDetails,pos)
}
override fun onFailure(call: Call<List<ModelClass>?>, t: Throwable) {
}
})
}
}
This is my adapter:
class RecyclerViewAdapterU (var dataList:ArrayList<ModelClass>): RecyclerView.Adapter<RecyclerViewAdapterU.ViewHolder>() {
var _binding: UploadItemViewBinding? = null
val binding get() = _binding!!
lateinit var v:View
var obj:upload_invent = upload_invent()
fun setData(listModel: ArrayList<ModelClass>,pos:Int) {
dataList[pos]=listModel[0]
notifyItemChanged(pos)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerViewAdapterU.ViewHolder {
v = LayoutInflater.from(parent.context).inflate(R.layout.upload_item_view, parent, false)
_binding = UploadItemViewBinding.bind(v)
return ViewHolder(binding.root)
}
override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
holder.bindItems(dataList[position])
}
override fun getItemCount(): Int {
return dataList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(data: ModelClass) {
binding.apply {
itemquant.text=data.item_quant
uploadItemName.text = data.item_name
uploadMfg.text = data.mfg
skuStock.setText(data.item_stock.toString())
skuCode.setText(data.sku_code)
}
updateStockDetail()
}
fun updateStockDetail(){
binding.skuCode.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
}
override fun afterTextChanged(editable: Editable) {
if (editable.length>=3){
//binding.skuCode.removeTextChangedListener(this)
var pos:Int=adapterPosition
var x= editable.toString().trim()
println("$x in textwatcher and $pos")
obj.updateStockDetail(editable.toString(),pos)
//notifyDataSetChanged()
//dataList[pos]=RecyclerViewAdapter.ob.dataSelected[pos]
notifyItemChanged(pos)
v.findNavController().navigate(R.id.action_upload_invent_refresh)
}}
})
}
}
}
note:ob.dataselected is from another recyclerview.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论