在此应用程序中,用户将输入项目详细信息并将其保存在SQLite数据库中。并在recyclerView中显示保存的项目。现在,我需要添加更多两个功能。 首先是从列表项中搜索。 第二个是使该项目可单击,以便单击该项目将进入详细视图活动。 这是我的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);
}
private void getItemsFromSQLite() {
//AsyncTask is used that sqlite operation not blocks the UI Thread
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
listItems.clear();
listItems.addAll(dbHelper.getAllItems());
return null;
}
protected void onPostExecute(Void params) {
super.onPostExecute(params);
itemAdapter.notifyDataSetChanged();
}
}.execute();
}
//Building Menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
final MenuItem item = menu.findItem(R.id.searchMenu);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
if (!TextUtils.isEmpty(s)) {
}
return true;
}
@Override
public boolean onQueryTextChange(String s) {
ArrayList<String> itemList = new ArrayList<>();
for (String items : itemList) {
if (items.toLowerCase().contains(s.toLowerCase())) ;
itemList.add(items);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(ItemListActivity.this,
android.R.layout.simple_list_item_1, itemList);
return true;
}
});
return true;
}
}
这是我的物品适配器类
package com.example.autoparts;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemHolder> {
Context iContext;
List<Items> itemsList;
public ItemAdapter(List<Items> itemsList, Context context) {
this.itemsList = itemsList;
iContext = context;
}
//Initiating viewHolder
@Override
public ItemHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v =
LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view, null);
//Holder
ItemHolder iHolder = new ItemHolder(v);
return iHolder;
}
@Override
public void onBindViewHolder(ItemHolder itemHolder, int i) {
Items item = itemsList.get(i);
itemHolder.itemNameTextView.setText(item.getItem());
itemHolder.itemVendorTextView.setText(item.getVendor());
itemHolder.itemQtyTextView.setText(Integer.toString(item.getQty()));
itemHolder.itemPriceTextView.setText(Integer.toString(item.getPrice()));
}
@Override
public int getItemCount() {
if (itemsList != null)
return itemsList.size();
else
return 0;
}
}
这是一个视图持有人类
package com.example.autoparts;
public class ItemHolder extends RecyclerView.ViewHolder {
// public final View view;
public TextView itemNameTextView;
public TextView itemVendorTextView;
public TextView itemQtyTextView;
public TextView itemPriceTextView;
public ItemHolder(View itemView) {
super(itemView);
itemNameTextView = (TextView)itemView.findViewById(R.id.item_name_text_view);
itemVendorTextView = (TextView)itemView.findViewById(R.id.item_vendor_text_view);
itemQtyTextView = (TextView) itemView.findViewById(R.id.item_qty_text_view);
itemPriceTextView = (TextView) itemView.findViewById(R.id.item_price_text_view);
}
}
这是菜单xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/searchMenu"
android:icon="@drawable/ic_action_search"
android:title="@string/search_title"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
</menu>
答案 0 :(得分:0)
首先在ItemAdapter类上方添加private ArrayList<Items> arraylist;
以进行初始化;
,并将此代码添加到类ItemAdapter中:
public void filterAll( String Qty ) {
itemsList.clear();
if (Qty.length() < 0) {
itemsList.addAll(arraylist);
} else {
for (Items ls : arraylist) {
if (ls.getqty().contains(qty) ) {
itemsList.add(ls);
}
}
notifyDataSetChanged();
}
现在在类ItemListActivity中的onQueryTextChange部分:
itemAdapter.filterall(s.tostring());
答案 1 :(得分:0)
在适配器内部:
public class ItemAdapter extends RecyclerView.Adapter<ItemHolder> implements Filterable {
private Context iContext;
private List<Items> itemsList = new ArrayList<>();
private List<Items> itemsListFilter = new ArrayList<>();
public ItemAdapter(List<Items> itemsList, Context context) {
this.itemsList = itemsList;
this.itemsListFilter = itemsList;
iContext = context;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
itemsList = (List<Items>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<Items> filteredList = new ArrayList<>();
String searchText = constraint.toString().toLowerCase();
String[] split = searchText.split(",");
ArrayList<String> searchGenres = new ArrayList<>(split.length);
for (String aSplit : split) {
// remove spaces
String trim = aSplit.trim();
// skip empty entries
if (trim.length() > 0)
searchGenres.add(trim);
}
for (Items dataNames : itemsListFilter) {
// filter by title
if (dataNames.getItem().toLowerCase().trim().contains(searchText)) {
filteredList.add(dataNames);
}
}
results.count = filteredList.size();
results.values = filteredList;
return results;
}
};
return filter;
}
}
在ItemListActivity
中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.YOUR_MENU, menu);
MenuItem item = menu.findItem(R.id.searchMenu);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
itemAdapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/searchMenu"
android:icon="@drawable/ic_action_search"
android:title="@string/search_title"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
</menu>