Android:无法使用CustomAdapter刷新Listview [英] Android: cannot refresh Listview using CustomAdapter

查看:120
本文介绍了Android:无法使用CustomAdapter刷新Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用新数据刷新列表视图.下面的代码用于获取第一次在FragmentActivity中的OnCreateView中的数据.

I need to refresh the list view with new data. This code below is used to obtain data in OnCreateView that is in FragmentActivity at the first time.

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
        val url = "something"

        val request_queue = Volley.newRequestQueue(this.context)
        val stringRequest = StringRequest(Request.Method.GET, url,
                Response.Listener<String> { response ->
                    val pending_job = Gson().fromJson<ArrayList<HashMap<String, String>>>(
                            response.toString(),
                            object : TypeToken<ArrayList<HashMap<String, String>>>() {}.type
                    )
                    this@PlaceholderFragment.showList(rootView, R.id.pending_job_list, pending_job)

                    pending_job_list_layout.setOnRefreshListener(this)

                    request_queue.stop()
                }, Response.ErrorListener { error ->
            if (error.networkResponse != null) {
                Toast.makeText(this.context, error.networkResponse.statusCode.toString(), Toast.LENGTH_SHORT).show()
            }
            request_queue.stop()
        })
        request_queue.add(stringRequest)
}

showList是用于设置CustomAdapter的功能,

showList is the function to set CustomAdapter, which is

fun showList(rootView: View, tab_id: Int, job_list: ArrayList<HashMap<String, String>>) {

    // custom display ListView
    val adapter: CustomAdapter = CustomAdapter(
            this.context,
            job_list
    )

    this_adapter = adapter

    val listView = rootView.findViewById(tab_id) as ListView
    listView.adapter = adapter
}

和这个CustomAdapter

class CustomAdapter(internal var mContext: Context,
                internal var job_list: ArrayList<HashMap<String, String>>
                ) : BaseAdapter() {

override fun getCount(): Int {
    return job_list.size
}

override fun getItem(position: Int): Any? {
    return null
}

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

override fun getView(position: Int, view: View?, parent: ViewGroup): View {
    val mInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

    val item_view = mInflater.inflate(R.layout.activity_job_item, parent, false)

    val job_name: MutableList<String> = arrayListOf()
    job_list.mapTo(job_name) { it["BaseSiteName"] as String }
    val nameView: TextView = item_view.findViewById(R.id.name) as TextView
    nameView.text = job_name[position]

    val job_date: MutableList<String> = arrayListOf()
    job_list.mapTo(job_date) { it["PlanDt"] as String}
    val dateView: TextView = item_view.findViewById(R.id.date) as TextView
    dateView.text = job_date[position]

    item_view.setOnClickListener{
        val intent: Intent = Intent(mContext, JobDetailActivity::class.java)
        intent.putExtra("job", job_list[position])
        mContext.startActivity(intent)
    }

    return item_view
}

}

但是,我想使列表可刷新.我编写了以下代码以刷新列表.

However, I want to make the list refresh-able. I've written the following code to refresh the list.

override fun onRefresh() {
    Toast.makeText(this.context, "Refresh", Toast.LENGTH_SHORT).show()
    val rootView = this_inflater!!.inflate(R.layout.fragment_pending_job, this_container, false)

    val url = "something"

    val request_queue = Volley.newRequestQueue(this.context)
    val stringRequest = StringRequest(Request.Method.GET, url,
            Response.Listener<String> { response ->
                val pending_job = Gson().fromJson<ArrayList<HashMap<String, String>>>(
                        response.toString(),
                        object : TypeToken<ArrayList<HashMap<String, String>>>() {}.type
                )

                val adapter: CustomAdapter = CustomAdapter(
                        this.context,
                        pending_job
                )

                val listView = rootView.findViewById(R.id.pending_job_list) as ListView
                listView.adapter = adapter

                pending_job_list_layout.isRefreshing = false

                pending_job_list_layout.setOnRefreshListener(this)

                request_queue.stop()
            }, Response.ErrorListener { error ->
        if (error.networkResponse != null) {
            Toast.makeText(this.context, error.networkResponse.statusCode.toString(), Toast.LENGTH_SHORT).show()
        }
        request_queue.stop()
    })
    request_queue.add(stringRequest)
}

那只是再次实例化CustomAdapter并将其再次带到列表视图.

That is just instantiating the CustomAdapter again and take it to the listview again.

刷新后,列表中的任何内容都不会更改.

When I refresh, nothing in the list is changed.

推荐答案

在CustomAdapter类中定义方法UpdateData,该方法将接收新数据并替换旧数据

Define a method UpdateData in CustomAdapter class which will receive new data and replaces old data

fun UpdateData(new_job_list: ArrayList<HashMap<String, String>>){
       job_list.clear()
       job_list.addAll(new_job_list)
       notifyDataSetChanged()
   }

现在,当您从volly获取新数据时,不必再次为listview设置适配器,只需使用

Now when you get new data from volly, instead of setting adapter again for listview just use

adapter.UpdateData(pending_job)

这篇关于Android:无法使用CustomAdapter刷新Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆