适配器的内容已更改,但ListView未收到通知。我该如何通知清单?

时间:2016-06-02 17:03:15

标签: android

这是我创建listview的片段。当数据发生变化时,listview不会通知。我怎样才能做到这一点?我使用volley在viewModel中发出请求。我收到下一个错误:java.lang.IllegalStateException:适配器的内容已更改但ListView未收到通知。确保不从后台线程修改适配器的内容,而只是从UI线程修改。确保适配器在其内容更改时调用notifyDataSetChanged()。 [在ListView(2131558620,类android.widget.ListView)中使用适配器(类pharma.com.pharmaapp.Common.EventsArrayAdapter)]                                                                             在android.widget.ListView.layoutChildren(ListView.java:1582)

         public class EventesFragment extends Fragment {
                private final String TAG = EventesFragment.class.getSimpleName();
                private ArrayList<EvenimentType> events = new ArrayList<>();
                private ListView eventsListView;
                private SwipeRefreshLayout swipeContainer;
                public EventsArrayAdapter eventsArrayAdapter;
                private EventsFragmentViewModel eventsFragmentViewModel = new EventsFragmentViewModel();


                @Nullable
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View view = inflater.inflate(R.layout.fragment_events, container, false);
                    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
                    fab.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                           Intent intent = new Intent(getActivity(), SearchActivity.class);
                            startActivity(intent);
                        }
                    });

                    if(events.size() != 0) {
                        events.clear();
                    }
                    eventsListView = (ListView) view.findViewById(R.id.listView_events);
                    swipeContainer = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer);
                    eventsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            String name = ((EvenimentType) eventsListView.getAdapter().getItem(position)).getName();
                            Log.d(TAG, "Click " + name);
                            Intent intent = new Intent(getActivity(), SelectedEventActivity.class);
                            intent.putExtra("Parceable", events.get(position));
                            startActivity(intent);
                        }
                    });

                    eventsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                        @Override
                        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                            String name = ((EvenimentType) eventsListView.getAdapter().getItem(position)).getName();
                            return false;
                        }
                    });

                    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            events.clear();
                            eventsFragmentViewModel.loadEvents(getActivity());
                            eventsFragmentViewModel.setInterface(new EventsFragmentViewModel.EventsFragmentInterface() {
                                @Override
                                public void takeData(ArrayList<EvenimentType> list) {
                                if(eventsArrayAdapter == null) {
                                    eventsArrayAdapter = new EventsArrayAdapter(getActivity(), list);
                                    eventsListView.setAdapter(eventsArrayAdapter);
                                    swipeContainer.setRefreshing(false);
                                     } else {
                                    events = list;
                                    getActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            eventsArrayAdapter.notifyDataSetChanged();
                                        }
                                    });

                                    swipeContainer.setRefreshing(false);
                                }
                                }
                            });
                        }
                    });


                    loadEvents();

                    return view;

                }

                public void loadElementsToArrayAdapter() {

                    eventsArrayAdapter = new EventsArrayAdapter(getContext(), events);
                   // eventsArrayAdapter.notifyDataSetChanged();
                    swipeContainer.setRefreshing(false);
                    eventsListView.setAdapter(eventsArrayAdapter);
                    eventsFragmentViewModel.saveEvents(events, getActivity());

                }

                public void loadEvents() {
                    eventsFragmentViewModel.loadEvents(getActivity());
                    eventsFragmentViewModel.setInterface(new EventsFragmentViewModel.EventsFragmentInterface() {
                        @Override
                        public void takeData(ArrayList<EvenimentType> list) {
                            events = list;
                            loadElementsToArrayAdapter();

                        }
                    });

                }

                @Override
                public void onResume() {
                    super.onResume();
                  //  eventsFragmentViewModel.getUserEventsId(getActivity());
                }


            }


    This is the Base Adapter class:  

    public class EventsArrayAdapter extends BaseAdapter {
        private static final String TAG = EventsArrayAdapter.class.getSimpleName();
        private Context context;
        private List<EvenimentType> list = null;


        public EventsArrayAdapter(Context context, List<EvenimentType> objects) {
            this.context = context;
            this.list = objects;
        }

        public void addAll(List<EvenimentType> objects) {
            if(list == null) {
                list = new ArrayList<>();
            }
            list.addAll(objects);
    notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if(convertView == null) {
                convertView = inflater.inflate(R.layout.item_array_adapter, null);
            }
            TextView title = (TextView) convertView.findViewById(R.id.text_simposion_title);
            TextView date = (TextView) convertView.findViewById(R.id.text_simposion_date);
            TextView location = (TextView) convertView.findViewById(R.id.text_simposion_location);
            TextView price = (TextView) convertView.findViewById(R.id.text_simposion_price);
            ImageView image = (ImageView) convertView.findViewById(R.id.image_event);
            Log.d(TAG, "Checking: " + checkEvent(list.get(position)));
            if (checkSelection(list.get(position)) == true) {
                title.setText(list.get(position).getName());
                date.setText(list.get(position).getDate());
                location.setText(list.get(position).getLocation());
                price.setText(list.get(position).getPaid());
                image.setImageResource(R.drawable.ic_simposions);

            } else {
                title.setText(list.get(position).getName());
                date.setText(list.get(position).getDate());
                location.setText(list.get(position).getLocation());
                price.setText(list.get(position).getPaid());
            }


            Log.d(TAG, "getView");

            return convertView;
        }


This is the viewModel where I make the request

public class EventsFragmentViewModel extends LoginViewModel {
    private static final String TAG = EventsFragmentViewModel.class.getSimpleName();
    private ArrayList<EvenimentType> events = new ArrayList<>();
    EventsFragmentInterface listener = null;

    public void loadEvents(final Context context) {
                DataServices dataServices = new DataServices(context);
                dataServices.getEvents();
                dataServices.setCommunicationInterface(new RequestToServer.CommunicationInterface() {
                    @Override
                    public void takeResponse(JSONObject response) {
                        try {
                            Log.d(TAG, response.toString());
                            EvenimentType evenimentType = new EvenimentType();
                            JSONArray obj = response.getJSONArray("entry");
                            Log.d(TAG, obj.length() + "");
                            for (int i = 0; i < obj.length(); i++) {
                                events.add(evenimentType.createEvent(obj.getJSONObject(i)));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                Log.d(TAG, "EVENTS: " + events.size());

                if(listener!= null) {
                    listener.takeData(events);
                }

            }

        });


    }

    public void saveEvents(ArrayList<EvenimentType> list, Context context) {
        Preferences preferences = new Preferences(context);
        preferences.saveToSharedPreferencesEvents(list);
        preferences.getFromSharedPreferencesEvents();
    }

    public void setInterface(EventsFragmentInterface listener) {
        this.listener = listener;
    }

   public interface EventsFragmentInterface {
        public void takeData(ArrayList<EvenimentType> list);
    }

}

0 个答案:

没有答案