如何使用SearchView筛选两个单独活动中的两个列表

时间:2018-09-03 11:31:31

标签: android android-recyclerview searchview

我在单独的活动中有两个单独的lists

工作日

MonthsActivity具有prepareMonthData()

private void prepareMonthData() {

    Calendar month = new Calendar("JANUARY");
    monthList.add(month);

    Calendar month = new Calendar("FEBRUARY");
    monthList.add(month);

    Calendar month = new Calendar("MARCH");
    monthList.add(month);

   //  APRIL, MAY, JUNE, JULY, AUG, SEP, OCT , NOV and Finally....

   Calendar month = new Calendar("DECEMBER");
    monthList.add(month); 
}

WeeksActivity具有prepareWeeksData()

private void prepareWeeksData {

Calendar week = new Calendar("MONDAY");
    weekList.add(week);

Calendar week = new Calendar("TUESDAY");
    weekList.add(week);

// Wednesday, Thursday, Friday, Saturday and Finally ...

Calendar week = new Calendar("SUNDAY");
    weekList.add(week);
}

我在以下两个活动中都实现了搜索视图

OnCreate方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.months);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    // editTextSearch = (EditText) findViewById(R.id.editTextSearch);

    mAdapter = new Adapter(this,monthList, this);
    RecyclerView.LayoutManager mLayoutManager = new 
    LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);


    // row click listener
    recyclerView.addOnItemTouchListener(new 
    RecyclerTouchListener(getApplicationContext(), recyclerView, new 
    RecyclerTouchListener.ClickListener() {

        @Override
        public void onClick(View view, int position) {
            Calendar month = mAdapter.getMonthsList().get(position);
            months = month.getTitle();
            Toast.makeText(getApplicationContext(), months + " is selected!", Toast.LENGTH_SHORT).show();
            view.getContext().startActivity(new Intent(view.getContext(),Months.class));

        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

    prepareMonthData();
}

onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.actionsearch)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    searchView.setMaxWidth(Integer.MAX_VALUE);
    return super.onCreateOptionsMenu(menu);

    // listening to search query text change
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            // filter recycler view when query submitted
            mAdapter.getFilter().filter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            // filter recycler view when text is changed
            mAdapter.getFilter().filter(query);
            return false;
        }
    });
    return true;
}    

..它完美搜索月份。.而其他活动则完美搜索星期..

现在,我想添加一个搜索视图,其中可以在我的MainActivity中搜索两个列表(月和周日)。即,如果用户搜索JANUARY或MONDAY,则该列表应过滤并显示结果..当列表处于单独的活动中时如何执行此操作...列表位于prepareMonthData()中的名称MonthsActivityprepareWeekData() WeeksActivity的方法中>

PS:我尝试通过intent方法中的MonthsActivityWeeksActivity的String查询传递给onQueryTextChange,但是当TextChanges时它会启动WeeksActivity ...

0 个答案:

没有答案