Android: 无法使用 CustomAdapter 刷新 Listview
Android: cannot refresh Listview using CustomAdapter
我需要用新数据刷新列表视图。下面这段代码用于第一次获取FragmentActivity
中的OnCreateView
中的数据。
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的函数,也就是
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,
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
}
}
但是,我想让列表可以刷新。我写了下面的代码来刷新列表。
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
并再次将其带到列表视图。
当我刷新时,列表中的任何内容都没有改变。
1.Way:
您可以使用 Asynctask
设置后台侦听器。您可以在一个循环中侦听您的服务器 continuous.When 数据更改,而不是刷新 listview
。这种方式很简单,但不利于性能。
2.Way:
您可以使用 firebase
database.Because 它是实时工作的。
3.Way:
如果你不想改变你的 database.You 可以使用 Google Cloud Messaging
。你必须实现这个,服务器和你的 application.When 数据都在服务器上更改监听并使用云消息发送消息到 android app.And 自动刷新 list view
。
在 CustomAdapter class 中定义一个方法 UpdateData
,它将接收新数据并替换旧数据
fun UpdateData(new_job_list: ArrayList<HashMap<String, String>>){
job_list.clear()
job_list.addAll(new_job_list)
notifyDataSetChanged()
}
现在,当您从 volly 获取新数据时,无需再次为列表视图设置适配器,只需使用
adapter.UpdateData(pending_job)
我需要用新数据刷新列表视图。下面这段代码用于第一次获取FragmentActivity
中的OnCreateView
中的数据。
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的函数,也就是
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,
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
}
}
但是,我想让列表可以刷新。我写了下面的代码来刷新列表。
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
并再次将其带到列表视图。
当我刷新时,列表中的任何内容都没有改变。
1.Way:
您可以使用 Asynctask
设置后台侦听器。您可以在一个循环中侦听您的服务器 continuous.When 数据更改,而不是刷新 listview
。这种方式很简单,但不利于性能。
2.Way:
您可以使用 firebase
database.Because 它是实时工作的。
3.Way:
如果你不想改变你的 database.You 可以使用 Google Cloud Messaging
。你必须实现这个,服务器和你的 application.When 数据都在服务器上更改监听并使用云消息发送消息到 android app.And 自动刷新 list view
。
在 CustomAdapter class 中定义一个方法 UpdateData
,它将接收新数据并替换旧数据
fun UpdateData(new_job_list: ArrayList<HashMap<String, String>>){
job_list.clear()
job_list.addAll(new_job_list)
notifyDataSetChanged()
}
现在,当您从 volly 获取新数据时,无需再次为列表视图设置适配器,只需使用
adapter.UpdateData(pending_job)