How to Refresh ListView in Fragment that is filled with BaseAdapter?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 05:39:11

You can do this

public void refresh(){
     adapter = new RegisterListAdapter(getActivity());
     list.setAdapter(adapter);
}

to refresh the listView or keep the data in memory with a different adapter.

EDIT: Sorry I didn't read well your question the first time, you can write this in your adapter:

public void addNewRow(String row){
    this.data.add(row);
    this.notifyDataSetChanged();
}
matreshkin

Try ListView.invalidateViews() refresh content

I use Interface methods. create one Interface class

interface RefreshListview {
fun onValueChanged(myList: ArrayList<ModelTask>)}

In your Activity class implement the Interface and its method

class MenuAct : AppCompatActivity(), View.OnClickListener,RefreshListview

Inside the method take the changed list and set to your CustomAdapter

 override fun onValueChanged(myList: ArrayList<ModelTask>) {
        notesAdapter = AdapterTask(this, myList) // handle your own Adapter and listview `static` initilisations 
        lv_act_menu_daily.adapter = notesAdapter    }

Ohk now we are inside DialogFragment class

private var mCallback: RefreshListview? = null

This is importand

   override fun onAttach(activity: Activity?) {
    super.onAttach(activity)
    try {
        mCallback = activity as RefreshListview?
    } catch (e: ClassCastException) {
        Log.d("MyDialog", "Activity doesn't implement the RefreshListview interface")
    }
}

Inside onActivityCreated

try {
            // handle your actions
            dBHelper.deleteTaskByID(myProg.id!!)
            //get the current listview
            var daily = dBHelper.readAllTask(Util.getInstance(context).currentTime)
            //send it to Interface method  
            mCallback?.onValueChanged(daily)
            dismiss()
        }catch (e:Exception){
            e.printStackTrace()
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!