简体   繁体   中英

Why isn't getView being called for every position?

I have a simple BaseAdapter for a GridView that of course calls getView for every position in the Grid when clicking on a cell within the Grid. For the most part, getView is called for every position in view (as it should), but sometimes it is only called for the first position. I need this method called for every position in view in order to highlight the selected cell and unhighlight the others but since it is not called for every position, it does not unhighlight the unselected cells.

getView method:

class AdapterSelectReference(context: Context, list: List<String>) : BaseAdapter()
{
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View
    {
        Logger.log(C.LOG_I, "~*ADAPTER_SELECT_REFERENCE", object{}.javaClass.enclosingMethod?.name + ": start: position = " + position)

        val view: View
        val lrh: ListRowHolder
        if(convertView == null)
        {
            view = this.mInflator.inflate(R.layout.grid_cell_layout, parent, false)
            lrh = ListRowHolder(view)
            view.tag = lrh
        }
        else
        {
            view = convertView
            lrh = view.tag as ListRowHolder
        }
        lrh.label.text = list[position]
        if(position != Settings.referenceSelected.book)
            lrh.label.setBackgroundColor(Color.parseColor("#ffffff"))
        else
            lrh.label.setBackgroundColor(Color.parseColor("#E0E7E7"))
        return view
    }
}

Activity:

class ActivitySelectReference : AppCompatActivity()
{
    lateinit var gvBooks: GridView
    lateinit var adapterBooks: AdapterSelectReference
    lateinit var listBooks: List<String>

    override fun onCreate(savedInstanceState: Bundle?)
    {
        listBooks = resources.getStringArray(R.array.bible_books).toList()
        gvBooks = findViewById(R.id.references_grid_view_books) as GridView
        adapterBooks = AdapterSelectReference(this, listBooks)
        gvBooks.setAdapter(adapterBooks)
        ...
    }   
...
}

The log print out that shows only position 0 is called on a certain touch:

2020-01-15 09:23:16.376 26646-26646/com.samuelriesterer.custombiblereadingplan I/~*ADAPTER_SELECT_REFERENCE: getView: start: position = 0
2020-01-15 09:23:16.393 26646-26646/com.samuelriesterer.custombiblereadingplan I/chatty: uid=10283(u0_a283) com.samuelriesterer.custombiblereadingplan identical 1 line
2020-01-15 09:23:16.399 26646-26646/com.samuelriesterer.custombiblereadingplan I/~*ADAPTER_SELECT_REFERENCE: getView: start: position = 0

Instead of setting background color to TextView , set on view like

if(position != Settings.referenceSelected.book)
    view.setBackgroundColor(Color.parseColor("#ffffff"))
else
    view.setBackgroundColor(Color.parseColor("#E0E7E7"))

This will fix the highlight/unhighlight problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM