-1

My code below fails when I call adapter.notifyDataSetChanged(). I got this code from an example. Not important to the question but I am filling an editText box with a scanned barcode. I then try to add it to the listview. I want to keep adding barcodes to the listview.

From the stack trace I believe this is the error: FATAL EXCEPTION: main Process: com.example.barcodereaderwedge, PID: 31252 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

class MainActivity : AppCompatActivity() {

    private lateinit var editText: EditText
    private lateinit var listView: ListView
    private lateinit var adapter: ArrayAdapter<String>
    private val itemList = mutableListOf<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        listView = findViewById(R.id.myListView)

        editText = findViewById(R.id.editTextText)
        editText.isFocusable = true
        editText.requestFocus()

        adapter = ArrayAdapter(this, R.layout.item_view, itemList)
        listView.adapter = adapter

        editText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                // This method is called before the text is changed.
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                // This method is called when the text is changed.
                //Toast.makeText(this@MainActivity, "Text changed: $s", Toast.LENGTH_SHORT).show()
            }

            override fun afterTextChanged(s: Editable?) {
                //just using a dummy value for testing
                val newItem = "test"
                if (newItem.isNotEmpty()) {
                    itemList.add("newItem")
                    adapter.notifyDataSetChanged()
                    editText.requestFocus()
                }
            }
        })
    }
}
2
  • 2
    Use Logcat to examine the stack trace associated with your crash. If you do not understand the stack trace, please copy and paste the text of the stack trace into your question. Commented May 8 at 22:06
  • 1
    See here for more: stackoverflow.com/questions/23353173/… Commented May 8 at 22:13

1 Answer 1

0

I solved my problem. Instead of using:

adapter = ArrayAdapter(this, R.layout.item_view, itemList)

Use:

adapter = ArrayAdapter(this,R.layout.item_view,R.id.itemTextView,itemList)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.