6

का उपयोग करके लिस्टव्यू रीफ्रेश नहीं कर सकता है मुझे नए डेटा के साथ सूची दृश्य को ताज़ा करने की आवश्यकता है। नीचे दिया गया कोड OnCreateView में डेटा प्राप्त करने के लिए उपयोग किया जाता है जो कि पहली बार FragmentActivity में है।एंड्रॉइड: कस्टमएडाप्टर

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 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 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 instantiating और यह सूचीदृश्य के लिए फिर से ले रहा है यही कारण है कि।

जब मैं रीफ्रेश करता हूं, तो सूची में कुछ भी नहीं बदला जाता है।

+0

ताज़ा होने पर आप अपने टुकड़े के दृश्य को क्यों बढ़ाते हैं? आपको इसे 'onCreateView()' भाग पर फुला देना चाहिए। संभवतः आपने अपनी प्रतिक्रिया में लिस्ट व्यू संदर्भ लिया है। गलत रूट से लिस्टनर (यानी जिसे आपने हाल ही में फुलाया था) और यही कारण है कि अपडेट दिखाई नहीं दे रहा है। (आपकी स्क्रीन पुराने रूट व्यू का उपयोग करती है और आपने इसके लिए कुछ भी नहीं किया है)। –

+1

यह खराब कार्यान्वयन है, आप डेटा सार्वजनिक के साथ ArrayList क्यों नहीं बनाते हैं और इसे फिर से सेट करते हैं? मेरा मतलब यह है: adapter.job_list = pending_job और फिर अधिसूचना कॉल करेंडेटाट चेंज()? – mayosk

+0

मैं 'onRefresh' में' adapter.job_list' जोड़ता हूं और फिर 'adapter.notifyDataSetChanged()' कहता हूं, इसलिए यह काम करता है! मेरी मदद करने के लिए सभी को धन्यवाद। – gameraroma

उत्तर

0

CustomAdapter कक्षा में एक विधि 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) 
0

1.Way: आप आसान लेकिन प्रदर्शन के लिए अच्छा नहीं है Asynctask के साथ पृष्ठभूमि श्रोता बना सकते हैं है.आप आप से बदल listview है इस तरह से ताज़ा कर सकते हैं एक पाश continuous.When डेटा में अपने सर्वर पर सुन सकते हैं।

2.Way: आप firebase डेटाबेस का उपयोग कर सकते हैं। क्योंकि यह रीयलटाइम काम कर रहा है।

3.Way: आप बदलना आपके database.You Google Cloud Messaging का उपयोग है.आप इस को लागू करना चाहिए कर सकते हैं नहीं करना चाहते हैं, दोनों सर्वर और आपके आवेदन करने डेटा सर्वर पर बदल सुनने और Android एप्लिकेशन को मेघ संदेश के साथ संदेश भेजने और स्वचालित रूप से list view रीफ्रेश करें।