So I am trying to scan multiple QR barcodes one by one and add them into the recyclerview list. I can scan one barcode, but upon the next scan the previous data gets overwritten by the new data, instead of adding it to the list. If I implement a list programmatically it populates the list fine so it's not a layout issue. It was suggested that I may have an issue where I may have put findViewById on a "view" inside recyclerview which could be the reason it only shows one instance of the data.
class QRAdapter (private var list: List<QRModel>) : RecyclerView.Adapter<QRAdapter.ViewHolder>()
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val qrTagID: TextView = view.findViewById(R.id.qrTagID)
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
// Create a new view, which defines the UI of the list item
val view = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.recyclerview_rfid, viewGroup, false)
return ViewHolder(view)
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val newList = list[position]
viewHolder.qrTagID.text = newList.qrTagID
}
override fun getItemCount() = list.size
}
data class QRModel(val qrTagID: String)`
class QRData : AppCompatActivity(){
private lateinit var recyclerView: RecyclerView
private lateinit var list : ArrayList<QRModel>
private var qrAdapter: QRAdapter? = null
private var qrTagCount : TextView? = null
private lateinit var qrTagHeader : TextView
private lateinit var qrScanRoom: ImageView
//private var qrTagId: TextView? = null
private var gridLayoutManager: GridLayoutManager? = null
@SuppressLint("SetTextI18n", "NotifyDataSetChanged")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_qr_data)
// Resources
qrTagCount = findViewById(R.id.qrTagCount)
qrTagHeader = findViewById(R.id.qrTagHeader)
recyclerView = findViewById(R.id.qrRecyclerView)
qrScanRoom = findViewById(R.id.scan_qr_room_white)
//qrTagId = findViewById(R.id.qrTagID)
// QR Scan Room Location onClick listener
qrScanRoom.setOnClickListener {
val intentIntegrator = IntentIntegrator(this)
intentIntegrator.setBeepEnabled(false)
intentIntegrator.setOrientationLocked(false)
intentIntegrator.setCameraId(0)
intentIntegrator.setPrompt("SCAN")
intentIntegrator.setBarcodeImageEnabled(true)
intentIntegrator.initiateScan()
}
//RecyclerView Data
recyclerView.isNestedScrollingEnabled = false
recyclerView.setHasFixedSize(true)
recyclerView.addItemDecoration(DividerItemDecoration(this, GridLayoutManager.VERTICAL))
qrList()
recyclerView.layoutManager = GridLayoutManager(this, 3)
gridLayoutManager?.reverseLayout = false
gridLayoutManager?.stackFromEnd = true
}
@SuppressLint("NotifyDataSetChanged")
@Deprecated("Deprecated in Java")
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
if (result != null) {
if (result.contents == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show()
} else {
Log.d("QRData", "Scanned")
Toast.makeText(this, "Scan Successful!", Toast.LENGTH_SHORT)
.show()
val codeContent = result.contents
// Get only cage ID# from url
val code = codeContent.filter(Char::isDigit)
qrTagID?.text = String.format(code)
}
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
@SuppressLint("NotifyDataSetChanged")
private fun qrList() {
val list = ArrayList<QRModel>()
list.add(QRModel(""))
qrAdapter?.notifyDataSetChanged()
qrAdapter = QRAdapter(list)
recyclerView.adapter = qrAdapter
}