Unable to use LayoutInflater in custom adapter

后端 未结 4 1147
太阳男子
太阳男子 2021-01-02 10:48

I\'m looking into writing a custom adapter to populate a listview with 3 textviews per line. I\'ve found quite a bit of example code to do this, but the one that seemed the

4条回答
  •  长发绾君心
    2021-01-02 11:32

    I believe this line is at fault:

    View v = inflater.inflate(R.layout.weather_row, null, true);
    

    You need instead:

    View v = inflater.inflate(R.layout.weather_row, parent, false);
    

    The false makes the inflated view independent of the parent, not attached to it, which very oddly seems to be the accepted design for custom views within AdapterViews. Why this is so, I find utterly baffling, but the pattern above worked for me.

提交回复
热议问题