如何在android中使用onOptionsItemSelected方法过滤listview?

时间:2015-09-14 12:16:33

标签: java android listview android-listview

我正在开发一个Android应用程序,因为我有3个选项卡,

             1.Invitation tab(it have the invitation listviews) 
             2.Event tab(it have the event listviewes)
             3.Groupchat tab(it have both invitation and event listviewes) 

我在gropchat标签中有一个下拉列表(显示在仪表板布局上)有3个项目即(全部,事件,邀请)。现在我需要的是,我想根据下拉项目点击功能在Groupchat选项卡中过滤我的Listviews (例如,当点击“事件”项目时,我只需要显示事件列表视图)。我如何实现这一目标,请帮助我。

我的编程代码如下,

public class GroupChatFragment extends Fragment {


    private Context context;
    private ListView chatListView;
    private TextView chatTitle;
    private TextView chatPlace;
    private TextView chatDate;
    private String dateResult;
    private List<EventMO> groupEventMoList = new ArrayList<>();
    private List<EventMO> eventMOs = new ArrayList<>();
    private List<EventMO> invitationMOs = new ArrayList<>();
    private EventDelegates eventDelegates = new EventDelegates();
    private Gson gson = new Gson();
    private ProgressDialog prgDialog;
    private ChatDisplayAdapter chatDisplayAdapter;
    private ListView GroupEventView;
    private SharedPreferences sharedpreferences;
    private TextView eventList;
    private MessageMO messageMO = new MessageMO();

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.all:

                return true;
            case R.id.event:

                return true;
            case R.id.invitation:

                return true;

        }
        return super.onOptionsItemSelected(item);
    }

    public void Event() {
        context = getActivity().getApplicationContext();
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        final UserMO userMO = dbHelper.getRingeeUserData(1);
        eventMOs = gson.fromJson(eventDelegates.getAllEventFromUser(userMO, context), new TypeToken<List<EventMO>>() {
        }.getType());
        List<EventMO> groupEventMOList = new ArrayList<>();

        for (EventMO eventMO : eventMOs) {
            groupEventMOList.add(eventMO);

        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final View 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);
        final UserMO userMO = dbHelper.getRingeeUserData(1);
        prgDialog = new ProgressDialog(getActivity());
        prgDialog.setMessage("Please wait...");
        // Set Cancelable as False
        prgDialog.setCancelable(false);
        prgDialog.show();
        //inviation group for user
        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());
                invitationMOs = gson.fromJson(eventDelegates.getAllEventForUser(userMO, context), new TypeToken<List<EventMO>>() {
                }.getType());
                List<EventMO> groupEventMOList = new ArrayList<>();

                for (EventMO eventMO : eventMOs) {
                    groupEventMOList.add(eventMO);

                }
                for (EventMO eventMO : invitationMOs) {
                    groupEventMOList.add(eventMO);

                }
                return groupEventMOList;
            }

            @Override
            protected void onPostExecute(List<EventMO> groupEventMOList) {
                groupEventMoList = groupEventMOList;
                prgDialog.dismiss();
                DatabaseHelper dbHelper = new DatabaseHelper(context);
                //long totalInsertion = dbHelper.insertUserRelationTable(userMOs);
                //Toast.makeText(context, "total userMos size " + userMOs.size() + "total db insertion size " + totalInsertion, Toast.LENGTH_LONG).show();
                GroupEventView = (ListView) view.findViewById(R.id.chat_list_view);
                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.setTextColor(getResources().getColor(R.color.black));
                chatDate.setTextColor(getResources().getColor(R.color.black));
                chatTitle.setTextColor(getResources().getColor(R.color.black));
                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)

您应该为每个Spinner选择编写自定义适配器,您需要为每个Spinner选择一个类别,然后将所选类别的数据/值设置为Listview。