Android CustomListAdapter notifydatachange() is not working

后端 未结 1 1434
無奈伤痛
無奈伤痛 2020-12-22 02:10

I want to disply list of items using Custom ArrayAdapter.

I will insert data by clicking a button.

MainFragment.java

相关标签:
1条回答
  • 2020-12-22 02:18

    Change your adapter as follows

        private class CustomListAdapter extends ArrayAdapter<CustomModel> {
    
        private final Context context;
        private final List<CustomModel> list;
        private TextView name, message;
    
        public CustomListAdapter(Context context, int resource,
                List<CustomModel> list) {
            super(context, resource, list);
            this.context = context;
            this.list = list;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.single_row, parent, false);
    
            name = (TextView) view.findViewById(R.id.name);
            message = (TextView) view.findViewById(R.id.message);
            CustomModel obj = list.get(position);
    
            name.setText(obj.getName());
            message.setText(obj.getMessage());
    
            return view;
        }
    
        public void updateList(List<CustomModel> list) {
    
            this.list = list;
            notifyDataSetChanged();
    
        }
    }
    

    so add the method updateList in your adapter itself and call the method notifyDataSetChanged();

    so from your activity instead of calling notifyDataSetChanged(); call updateList(List<CustomModel> newList)

    I am sure this will work.

    0 讨论(0)
提交回复
热议问题