进行过滤操作后显示空列表视图

时间:2015-09-21 12:48:06

标签: android listview android-listview

我的布局中有一个下拉列表来过滤我的listviewes,我使用了onOptionsItemSelected方法来过滤我的listviewes,现在我使用displayEventList()方法成功过滤了我的EventListviewes。但遗憾的是,过滤后的listview没有显示在布局中(之后)过滤操作过滤的事件列表视图可能无法在布局中显示),请帮我解决这个问题。

    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d("onOptionsItemSelected", "yes");

        switch (item.getItemId()) {
            case R.id.all:
                displayEventInvitationList(inflater, container,savedInstanceState);
                return true;
            case R.id.event:
                displayEventList(inflater, container,savedInstanceState);
                return true;
            case R.id.invitation:
                displayInvitationList(inflater, container,savedInstanceState);
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }

    }

    public View displayEventList(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.chatwindow_tab, container, false);
        context = getActivity().getApplicationContext();
        sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        userMO = dbHelper.getRingeeUserData(1);
        new AsyncTask<Void, Void, List<EventMO>>() {
            @Override
            protected List<EventMO> doInBackground(Void... arg0) {
                eventMOs = gson.fromJson(eventDelegates.getAllEventFromUser(userMO, context), new TypeToken<List<EventMO>>() {
                }.getType());
                List<EventMO> groupEventList = new ArrayList<>();
                for (EventMO eventMO : eventMOs) {
                    groupEventList.add(eventMO);

                }
                return groupEventList;//it returns Eventlist
            }

            @Override
            protected void onPostExecute(List<EventMO> groupEventList) {
                groupEventMoList = groupEventList;
                chatDisplayAdapter = new ChatDisplayAdapter();
                groupEventView.setAdapter(chatDisplayAdapter);
                groupEventView.setItemsCanFocus(true);
                groupEventView.setTextFilterEnabled(true);
                groupEventView.setOnItemClickListener(groupEventView.getOnItemClickListener());
            }

        }.execute(null, null, null);
        return view;
    }
        private class ChatDisplayAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        public ChatDisplayAdapter() {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

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

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

        @Override
        public long getItemId(int id) {
            // for sqllite management
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.chatwindow, null);
                convertView.setClickable(true);
                convertView.setFocusable(true);
                chatTitle = (TextView) convertView.findViewById(R.id.chat_title);
                chatPlace = (TextView) convertView.findViewById(R.id.event_place);
                chatDate = (TextView) convertView.findViewById(R.id.event_date);
                chatPlace.setText(groupEventMoList.get(position).getPlace());
                String actualDate = groupEventMoList.get(position).getEventDate();
                //chatDate.setText(groupEventMoList.get(position).getEventDate());
                try {
                    //date format changed here
                    Date formatDate = new SimpleDateFormat("yyyy-MM-dd").parse(actualDate);
                    dateResult = new SimpleDateFormat("dd-MM-yyyy").format(formatDate);
                } catch (ParseException e) {

                    e.printStackTrace();
                }
                chatDate.setText(dateResult);
                chatTitle.setText(groupEventMoList.get(position).getText());
                convertView.setTag(position);
                View v = convertView.findViewById(R.id.chat_window_single);
                v.getRootView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i("position", v.getTag().toString());
                        Intent groupAct = new Intent(context, GroupChatActivity.class);
                        groupAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        groupAct.putExtra("eventMo", groupEventMoList.get(Integer.parseInt(v.getTag().toString())));
                        startActivity(groupAct);
                    }

                });
            }
            return convertView;
        }

    }

}

1 个答案:

答案 0 :(得分:0)

用于过滤具有更新列表的集合适配器并在Adpater上调用notifyDataSetChanged()

ChatDisplayAdapter chatDisplayAdapter =new ChatDisplayAdapter(groupEventList);
chatDisplayAdapter.notifyDataSetChanged(); 

<强> ChatDisplayAdpater.java

 private class ChatDisplayAdapter extends BaseAdapter<EventMO> {

        private LayoutInflater mInflater;
        List<EventMO> groupEventMoList;
        public ChatDisplayAdapter(List<EventMO> datagroupEventMoList) {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        groupEventMoList=datagroupEventMoList;
        }

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

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

        @Override
        public long getItemId(int id) {
            // for sqllite management
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.chatwindow, null);
                convertView.setClickable(true);
                convertView.setFocusable(true);
                chatTitle = (TextView) convertView.findViewById(R.id.chat_title);
                chatPlace = (TextView) convertView.findViewById(R.id.event_place);
                chatDate = (TextView) convertView.findViewById(R.id.event_date);
                chatPlace.setText(groupEventMoList.get(position).getPlace());
                String actualDate = groupEventMoList.get(position).getEventDate();
                //chatDate.setText(groupEventMoList.get(position).getEventDate());
                try {
                    //date format changed here
                    Date formatDate = new SimpleDateFormat("yyyy-MM-dd").parse(actualDate);
                    dateResult = new SimpleDateFormat("dd-MM-yyyy").format(formatDate);
                } catch (ParseException e) {

                    e.printStackTrace();
                }
                chatDate.setText(dateResult);
                chatTitle.setText(groupEventMoList.get(position).getText());
                convertView.setTag(position);
                View v = convertView.findViewById(R.id.chat_window_single);
                v.getRootView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i("position", v.getTag().toString());
                        Intent groupAct = new Intent(context, GroupChatActivity.class);
                        groupAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        groupAct.putExtra("eventMo", groupEventMoList.get(Integer.parseInt(v.getTag().toString())));
                        startActivity(groupAct);
                    }

                });
            }
            return convertView;
        }

    }