Tôi cần làm mới chế độ xem danh sách với dữ liệu mới. Mã dưới đây được sử dụng để lấy dữ liệu trong OnCreateView
có nghĩa là trong FragmentActivity
ở lần đầu tiên.Android: không thể làm mới Chế độ xem danh sách bằng CustomAdapter
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
)
[email protected](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
là chức năng để thiết lập CustomAdapter, đó là
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
}
, và CustomAdapter
lớp này,
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
}
}
Tuy nhiên, tôi muốn làm cho danh sách có thể làm mới. Tôi đã viết đoạn mã sau để làm mới danh sách.
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)
}
Chỉ cần khởi tạo lại CustomAdapter
và đưa lại vào chế độ xem danh sách.
Khi tôi làm mới, không có gì trong danh sách được thay đổi.
tại sao bạn thổi phồng chế độ xem đoạn của mình khi làm mới? bạn nên thổi phồng nó trên phần 'onCreateView()'. Có thể bạn đã lấy tham chiếu listView trong Response.Listener của bạn từ gốc sai (tức là gốc bạn vừa mới tăng) và đó là lý do tại sao bản cập nhật không hiển thị. (Màn hình của bạn sử dụng rootView cũ và bạn không làm gì với nó). –
đây là triển khai không tốt, tại sao bạn không tạo ArrayList với dữ liệu công khai và đặt lại? tôi có nghĩa là như thế này: adapter.job_list = pending_job và sau đó gọi notifyDataSetChanged()? – mayosk
Tôi chỉ cần thêm 'adapter.job_list' vào' onRefresh' và sau đó gọi 'adapter.notifyDataSetChanged()', vì vậy nó hoạt động! Cảm ơn mọi người đã giúp tôi. – gameraroma