如何搜索从SQLite数据库填充的回收站视图?

在此应用程序中,用户将输入项目详细信息并将其保存在SQLite数据库中。并在回收站视图中显示已保存的项目。现在我需要添加更多两个功能。首先是从列表项进行搜索。其次是使项目可单击,以便在单击项目时,它将进入详细信息视图活动。这是我的ItemListActivity java类文件


package com.example.autoparts;


public class ItemListActivity extends AppCompatActivity {


    Context context = ItemListActivity.this;


    private RecyclerView itemsRecycler;


    private ArrayList<Items> listItems;


    private SearchView iSearchView;


    private SearchManager manager;


    //To customize the layout property

    private RecyclerView.LayoutManager itemLayoutManager;

    //calling the ItemAdapter

    private ItemAdapter itemAdapter;

    //calling the dbhelper class

    PartsDbHelper dbHelper;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_item_list);


        //Recycler View for items to display

        itemsRecycler = (RecyclerView) findViewById(R.id.items_recycler_view);

        dbHelper = new PartsDbHelper(this);


        manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);


        //set the fixed size of the RecyclerView

        itemsRecycler.setHasFixedSize(true);

        //setting Linear as layout

        itemsRecycler.setLayoutManager(new LinearLayoutManager(this));

        itemAdapter = new ItemAdapter(listItems, this);


        generateObjects();

        getItemsFromSQLite();


    }



    private void generateObjects() {

        listItems = new ArrayList<>();

        itemAdapter = new ItemAdapter(listItems, this);



        RecyclerView.LayoutManager layoutManager = new 

        LinearLayoutManager(getApplicationContext());

        itemsRecycler.setLayoutManager(layoutManager);


        itemsRecycler.setAdapter(itemAdapter);


        dbHelper = new PartsDbHelper(this);

    }



慕桂英4014372
浏览 110回答 2
2回答

富国沪深

适配器内部:public class ItemAdapter extends RecyclerView.Adapter<ItemHolder> implements Filterable {&nbsp; &nbsp; private Context iContext;&nbsp; &nbsp; private List<Items> itemsList = new ArrayList<>();&nbsp; &nbsp; private List<Items> itemsListFilter = new ArrayList<>();&nbsp; &nbsp; public ItemAdapter(List<Items> itemsList, Context context) {&nbsp; &nbsp; &nbsp; &nbsp; this.itemsList = itemsList;&nbsp; &nbsp; &nbsp; &nbsp; this.itemsListFilter = itemsList;&nbsp; &nbsp; &nbsp; &nbsp; iContext = context;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Filter getFilter() {&nbsp; &nbsp; &nbsp; &nbsp; Filter filter = new Filter() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected void publishResults(CharSequence constraint, FilterResults results) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemsList = (List<Items>) results.values;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected FilterResults performFiltering(CharSequence constraint) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FilterResults results = new FilterResults();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Items> filteredList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String searchText = constraint.toString().toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] split = searchText.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> searchGenres = new ArrayList<>(split.length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String aSplit : split) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // remove spaces&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String trim = aSplit.trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // skip empty entries&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (trim.length() > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; searchGenres.add(trim);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Items dataNames : itemsListFilter) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // filter by title&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dataNames.getItem().toLowerCase().trim().contains(searchText)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filteredList.add(dataNames);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.count = filteredList.size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.values = filteredList;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return results;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; return filter;&nbsp; &nbsp; }}在:ItemListActivity@Overridepublic boolean onCreateOptionsMenu(Menu menu) {&nbsp; &nbsp; MenuInflater inflater = getMenuInflater();&nbsp; &nbsp; inflater.inflate(R.menu.YOUR_MENU, menu);&nbsp; &nbsp; MenuItem item = menu.findItem(R.id.searchMenu);&nbsp; &nbsp; SearchView searchView = (SearchView) item.getActionView();&nbsp; &nbsp; searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onQueryTextChange(String newText) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemAdapter.getFilter().filter(newText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onQueryTextSubmit(String query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; return super.onCreateOptionsMenu(menu);}菜单<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto">&nbsp; &nbsp; <item&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/searchMenu"&nbsp; &nbsp; &nbsp; &nbsp; android:icon="@drawable/ic_action_search"&nbsp; &nbsp; &nbsp; &nbsp; android:title="@string/search_title"&nbsp; &nbsp; &nbsp; &nbsp; app:actionViewClass="androidx.appcompat.widget.SearchView"&nbsp; &nbsp; &nbsp; &nbsp; app:showAsAction="collapseActionView|ifRoom" /></menu>

慕容708150

首先添加上面的类 ItemAdapter for intialize;private ArrayList<Items> arraylist;并将此代码添加到类 ItemAdapter 中:&nbsp; &nbsp;public void filterAll( String Qty ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemsList.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (Qty.length() < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemsList.addAll(arraylist);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Items ls : arraylist) {&nbsp;if (ls.getqty().contains(qty) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemsList.add(ls);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;notifyDataSetChanged();}现在在类 ItemListActivity in part onQueryTextChange 中:itemAdapter.filterall(s.tostring());&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java