为什么在调用notifyDataSetChanged时适配器gridviewList不起作用

时间:2019-01-06 08:13:26

标签: android kotlin

为什么在调用notifyDataSetChanged时适配器gridviewList不起作用

当滚动gridView项目显示时:(

//在这里,我在打开活动时称为onCreate有趣

var arraySection = ArrayList<section>()
var adapter:sectionsAdapter?=null


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    adapter = sectionsAdapter(arraySection,this)
    gridViewSections.adapter = adapter

    getData()
}

//在这里,我为网格视图制作了一个适配器

class sectionsAdapter:BaseAdapter{

    var arraySection = ArrayList<section>()
    var context:Context?=null

    constructor(arraySection:ArrayList<section> , context:Context) {
        this.arraySection = arraySection
        this.context = context
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        println("position " + position)
        val row = arraySection[position]

        val inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val viewRow = inflater.inflate(R.layout.view_grid_home , null)

       viewRow.textViewSectionId.text = row.title
        viewRow.imageViewSectionId.loadFromUrl(row.imageurl!! , R.drawable.iconapp)

        viewRow.setOnClickListener {
            val intent = Intent(context , LensesActivity::class.java)
            intent.putExtra("sectionId" , row.id)
            context!!.startActivity(intent)
        }

        return viewRow
    }

    override fun getItem(position: Int): Any {
        return arraySection[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        println("getCount " + arraySection.size)
        println("getCount this " + this.arraySection.size)
        return this.arraySection.size
    }

}

//这很有趣,可以通过以下方式从数据库加载所有数据 我加载所有数据后添加到数组  但我在网格视图中看不到任何数据  当我滚动网格视图时,将显示所有数据

fun getData(){
    arraySection.clear()

    val parameter =  Urls().makeParameter(tbl.sctions.name
        , 50
        , "id"
        , orderType.DESC.name
        , "{\"where\":\"isShow='1'\"}") // "+ isSections +"


    Fuel.post(Urls().getData)
        .body(parameter)
        .responseString { request , response , result ->


            if (response.isSuccessful) {
                val gson = GsonBuilder().create()
                val secionsData = gson.fromJson(result.get() , sectionList::class.java)

                for (s in secionsData.data) {
                    val row = section(s.id,s.title,s.isShow,s.imageUrl)
                    arraySection.add(row)
                }

                adapter!!.notifyDataSetChanged()
            }
        }
}

0 个答案:

没有答案