I want to search items in recyclerview. I am using addtextchangedlistener method for searching. When I start typing in edittext to search an item instead of giving me the filtered results the app get closed and I got the error as
12-27 11:29:04.375 18962-18962/com.example.satyabhai.restaurant E/RecyclerView: No adapter attached; skipping layout
12-27 11:31:17.932 18962-18962/com.example.satyabhai.restaurant E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.satyabhai.restaurant, PID: 18962
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
at com.example.satyabhai.restaurant.CurrentStatus.filter(CurrentStatus.java:121)
at com.example.satyabhai.restaurant.CurrentStatus.access$000(CurrentStatus.java:40)
at com.example.satyabhai.restaurant.CurrentStatus$1.afterTextChanged(CurrentStatus.java:95)
This is code for my edittext search
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
This is code for my Filter method
private void filter(String s) {
ArrayList<CurrentEntry> temp=new ArrayList<>();
for(CurrentEntry d: current)
{
if(d.getName().toLowerCase().contains(s)||d.getNo().toLowerCase().contains(s)||d.getPeople().toLowerCase().contains(s)||d.getEstimate().toLowerCase().contains(s));
{
temp.add(d);
}
}
adapt.updateList(temp);
}
This is code for UpdateList
public void updateList(ArrayList<CurrentEntry> temp)
{
this.listItems=temp;
notifyDataSetChanged();
}
This is MainActivity code for setting recyclerview
RecyclerView recyclerView;
NewAdapter adapt;
private ArrayList<CurrentEntry> current;
String userUrl;
String second_req="second_req";
EditText search;
public CurrentStatus() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.current_status,container,false);
FloatingActionButton fab=(FloatingActionButton)v.findViewById(R.id.fab);
current=new ArrayList<>();
recyclerView=(RecyclerView)v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
search=(EditText)v.findViewById(R.id.search);
This line is gettting Issue
if(!TextUtils.isEmpty(d.getName())|| !TextUtils.isEmpty(d.getPeople())||!TextUtils.isEmpty(d.getNo())|| !TextUtils.isEmpty(d.getEstimate())) {
if (d.getName().toLowerCase().contains(s) || d.getNo().toLowerCase().contains(s) || d.getPeople().toLowerCase().contains(s) || d.getEstimate().toLowerCase().contains(s));
{
temp.add(d);
}
This is code for Adapter
public class NewAdapter extends RecyclerView.Adapter<NewAdapter.ViewHolder> {
ArrayList<CurrentEntry> listItems;
Context context;
public NewAdapter (ArrayList<CurrentEntry> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
public void updateList(ArrayList<CurrentEntry> temp)
{
this.listItems=temp;
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_card,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
CurrentEntry current=listItems.get(position);
holder.number.setText(current.getNo());
holder.Name.setText(current.getName());
holder.People.setText(current.getPeople());
holder.Estimate.setText(current.getEstimate());
// holder.Foodie.setText(current.getFoodie());
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView number;
TextView Name ;
TextView People;
TextView Estimate;
// TextView Foodie;
public ViewHolder(View itemView) {
super(itemView);
number=(TextView)itemView.findViewById(R.id.no);
Name=(TextView)itemView.findViewById(R.id.name);
People=(TextView)itemView.findViewById(R.id.people);
Estimate=(TextView)itemView.findViewById(R.id.estimate);
// Foodie=(TextView)itemView.findViewById(R.id.foodie);
}
}
@Override
public int getItemCount() {
return listItems.size();
}
}