我正在尝试在searchview
中实施recycleview
,但这不起作用
没有任何类型的错误显示,我无法弄清楚我的错误在哪里。
这是包含search activity
和适配器类的recycleview
。我只需创建演示来搜索recycleview
中的项目:
SearchActivity.java
public class New_Search extends AppCompatActivity
{
private RecyclerView recyclerView;
DummyAdapter mAdapter;
List<Lead_Contacts> movieList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dummysearch);
movieList=new ArrayList<>();
Lead_Contacts contacts = new Lead_Contacts("Mad Max: Fury Road", "Action & Adventure", "29/04/2016");
movieList.add(contacts);
contacts= new Lead_Contacts("Inside Out", "Animation, Kids & Family", "29/04/2016");
movieList.add(contacts);
contacts = new Lead_Contacts("Star Wars: Episode VII - The Force Awakens", "Action", "29/04/2016");
movieList.add(contacts);
contacts= new Lead_Contacts("Shaun the Sheep", "Animation", "29/04/2016");
movieList.add(contacts);
contacts= new Lead_Contacts("Shaun the Sheep", "Animation", "29/04/2016");
movieList.add(contacts);
contacts= new Lead_Contacts("Shaun the Sheep", "Animation", "29/04/2016");
movieList.add(contacts);
contacts= new Lead_Contacts("Shaun the Sheep", "Animation", "29/04/2016");
movieList.add(contacts);
movieList.add(contacts);
recyclerView = (RecyclerView)findViewById(R.id.recycler_view1);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter=new DummyAdapter(getApplicationContext(),movieList);
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
public class DummyAdapter extends RecyclerView.Adapter<DummyAdapter.Customview_holder>
{
private Context context;
private List<Lead_Contacts> listcontacts;
Lead_Contacts leadcontacts;
List<Lead_Contacts> leadlist;
// ClickListener listener;
public CardView mCardView;
public DummyAdapter(Context context, List<Lead_Contacts> listcontacts)
{
this.context=context;
this.listcontacts=listcontacts;
}
@Override
public Customview_holder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.seachrecyclelis, parent, false);
//Customview_holder viewHolder = new Customview_holder(view);
return new Customview_holder(view);
}
@Override
public void onBindViewHolder(Customview_holder customViewHolder, int position)
{
//customViewHolder.mCardView.setTag(position);
// System.out.println("call binder method");
Lead_Contacts leadcontacts = listcontacts.get(position);
customViewHolder.cust_name_textView.setText(leadcontacts.getCustomer_name());
customViewHolder.eventname_textView.setText(leadcontacts.getEvent_name());
customViewHolder.date_textView.setText(leadcontacts.getDatetime());
}
public int getItemCount()
{
return listcontacts.size();
}
public void filter(String trim) {
}
public class Customview_holder extends RecyclerView.ViewHolder {
//public View mCardView;
protected TextView cust_name_textView;
protected TextView eventname_textView;
protected TextView date_textView;
public Customview_holder(View itemView)
{
super(itemView);
// mCardView = (CardView) itemView.findViewById(R.id.cardview);
// mCardView.setOnClickListener(this);
System.out.println("call binder method");
this.cust_name_textView = (TextView)itemView.findViewById(R.id.lead_cust_name);
this.eventname_textView = (TextView)itemView.findViewById(R.id.lead_eventname);
this.date_textView = (TextView)itemView.findViewById(R.id.leaddate);
}
// public void onClick(View v)
// {
// int position = (int) v.getTag();
// Toast.makeText(v.getContext(),Integer.toString(position), Toast.LENGTH_SHORT).show();
//
// }
}
}
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.lead_search, menu);
super.onCreateOptionsMenu(menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
//*** setOnQueryTextFocusChangeListener ***
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus)
{
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query)
{
return false;
}
@Override
public boolean onQueryTextChange(String searchQuery) {
mAdapter.filter(searchQuery.toString().trim());
recyclerView.invalidate();
return true;
}
});
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_search)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是将menu.xml
放在searchview
上的actionbar
文件:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/action_search"
android:icon="@drawable/search"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"
/>
</menu>
答案 0 :(得分:0)
您忘记实施过滤方法:
public void filter(String trim) {
}
首先在DummyAdapter中声明一个已过滤的列表:
private List<Lead_Contacts> filteredContactList;
然后更改构造函数以获取列表的副本:
public DummyAdapter(Context context, List<Lead_Contacts> listcontacts)
{
this.context=context;
this.listcontacts=listcontacts;
this.filteredContactList = new ArrayList<>(listcontacts);
}
更改你的getItemCount以返回已过滤列表的大小:
public int getItemCount()
{
return filteredContactList.size();
}
然后实现您的过滤方法:
public void filter(String trim) {
filteredContactList.clear();
for (Lead_Contacts contact : listcontacts) {
if (contact. getCustomer_name().toLowerCase().contains(trim.toLowerCase())) {
filteredContactList.add(contact);
}
}
}