how to refresh custom listview using baseadapter in android

后端 未结 5 757
无人及你
无人及你 2020-12-08 23:37

sir, how can i refresh my custom listview using baseadapter. i don\'t know what to place, or where to place it in my code. please help me. thanks in advance



        
5条回答
  •  轮回少年
    2020-12-09 00:12

    Two options: either hold onto the reference for the ArrayList that you passed into the constructor so you can modify the actual list data later (since the list isn't copied, modifying the data outside the Adapter still updates the pointer the Adapter is referencing), or rewrite the Adapter to allow the list to be reset to another object.

    In either case, after the ArrayList has changed, you must call notifyDataSetChanged() to update your ListView with the changes. This can be done inside or outside the adapter. So, for example:

    public class MyCustomBaseAdapter extends BaseAdapter {
        //TIP: Don't make this static, that's just a bad idea
        private ArrayList searchArrayList;
    
        private LayoutInflater mInflater;
    
        public MyCustomBaseAdapter(Context context, ArrayList initialResults) {
            searchArrayList = initialResults;
            mInflater = LayoutInflater.from(context);
        }
    
        public void updateResults(ArrayList results) {
            searchArrayList = results;
            //Triggers the list update
            notifyDataSetChanged();
        }
    
        /* ...The rest of your code that I failed to copy over... */
    }
    

    HTH

提交回复
热议问题