i am implementing search filter using search view i loaded list
successfully and when i clicked on searchview and type word
application crash below is error is Here's the code :
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
// private static final String url = "http://example.com/OnlineDoctor/mobileapp/doctors_list.php?";
private List<ListItems> listItemses;
private RecyclerView recyclerView;
private MainFilteredAdapter mrecyclerViewAdapter;
private LinearLayoutManager linearLayoutManager;
private static final String TAG = MainActivity.class.getSimpleName();
private RelativeLayout ownerlayout;
private SearchView searchView;
private String logintype, property_id;
private String userid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
listItemses = new ArrayList<>();
mrecyclerViewAdapter = new
MainFilteredAdapter(listItemses,getApplicationContext());
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mrecyclerViewAdapter);
loadRenterList();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
mrecyclerViewAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
mrecyclerViewAdapter.getFilter().filter(query);
return false;
}
});
return true;
}
public void loadRenterList() {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
JSONArray jsonArray = null;
JSONObject jsonObject = null;
try {
jsonArray = response.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mjsonObject = (JSONObject) jsonArray.get(i);
ListItems newslist = new ListItems();
newslist.setId(mjsonObject.getString("id"));
listItemses.add(newslist);
mrecyclerViewAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
// adapter.notifyDataSetChanged();
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
mrecyclerViewAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
mrecyclerViewAdapter.getFilter().filter(query);
return false;
}
});
return true;
}
public class MainFilteredAdapter extends RecyclerView.Adapter<MainFilteredAdapter.MyViewHolder> implements Filterable {
private List<ListItems> doctorlist;
private List<ListItems> doctorlistFiltered;
Context context;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView spec_name,placeName,property_status;
public ImageView imageView;
LinearLayout lllistrow;
public MyViewHolder(View itemView) {
super(itemView);
property_status = itemView.findViewById(R.id.duration);
spec_name = itemView.findViewById(R.id.spec_name);
placeName = itemView.findViewById(R.id.placeName);
imageView = itemView.findViewById(R.id.placeImage);
lllistrow= itemView.findViewById(R.id.mainHolder);
lllistrow.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int position = getLayoutPosition();
switch (v.getId()) {
case R.id.mainHolder:
break;
}
}
}
public MainFilteredAdapter(List<ListItems> boardList, Context context) {
this.doctorlist = boardList;
this.doctorlistFiltered=boardList;
this.context = context;
}
#Override
public MainFilteredAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem, parent, false);
return new MainFilteredAdapter.MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MainFilteredAdapter.MyViewHolder holder, int position) {
ListItems listItems = doctorlistFiltered.get(position);
/*Picasso.with(context).load("http://neweraprime.com/SFDS/faculty/Upload/Books/" + newslist.get(position).getThumbnailUrl())
.placeholder(R.drawable.ic_pdffile).error(R.drawable.ic_pdffile).resize(256, 256).onlyScaleDown() //Url of the image to load.
.into(holder.thumbNail);*/
holder.property_status.setText(listItems.getPayment_status());
holder.spec_name.setText(listItems.getAddress());
holder.placeName.setText(listItems.getTitle());
}
#Override
public Filter getFilter()
{
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
doctorlistFiltered = doctorlist;
} else {
List<ListItems> filteredList = new ArrayList<>();
for (ListItems row : doctorlist) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getCity().toLowerCase().contains(charString.toLowerCase()) || row.getAddress().contains(charSequence)) {
filteredList.add(row);
}
}
doctorlistFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = doctorlistFiltered;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
doctorlistFiltered = (ArrayList<ListItems>) filterResults.values;
notifyDataSetChanged();
}
};
}
#Override
public int getItemCount() {
if(doctorlist != null){
return doctorlistFiltered.size();
} else {
return 0;
}
}
and here is the error
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.www.homerent.MainFilteredAdapter.getItemCount(MainFilteredAdapter.java:129)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4025)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3830)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4385)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
I'd suspect your problem is here:
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
doctorlistFiltered = (ArrayList<ListItems>) filterResults.values;
notifyDataSetChanged();
}
Try using this instead:
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
doctorlistFiltered = new ArrayList<ListItems>();
if (filterResults.values != null) {
doctorlistFiltered.addAll((ArrayList<ListItems>) filterResults.values);
}
notifyDataSetChanged();
}
I haven't seen anything that guarantees that filterResults.values does contain a non null value(e.g. in case of an error during filtering).
Ps.:
Adjusting the getItemCount() as metionend in the comments should be a given.
Related
I made an app with listView and I want to use search in top bar but it doesn't work.
In this program, I get the data from the firebase.
These are the codes below:
This is MainActivity code:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int List_APP_LOADER = 0;
ArrayList<Informatin> listItem;
ListView listView;
listAdapter adapter;
DatabaseReference reference;
ArrayList<Informatin> mInformation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItem = new ArrayList<>();
listView = findViewById(R.id.list);
LoaderManager.getInstance(this).initLoader(LIST_APP_LOADER, null, this);
mInformation = new ArrayList<>();
adapter = new listAdapter(this, mInformation);
listView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main_screen, menu);
MenuItem searchItem = menu.findItem(R.id.search_item);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
listView.getFilter().filter(newText);
return true;
}
});
return true;
}
}
This is listAdapter code
public class listAdapter extends ArrayAdapter<Informatin> implements Filterable {
List<Informatin> arrayList;
Context context;
public listAdapter(Context context, ArrayList<Informatin> informatins) {
super(context, 0, informatins);
this.context=context;
arrayList = informatins;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Informatin currentList = getItem(position);
TextView nameTextView = listItemView.findViewById(R.id.name);
TextView descriptionTextView = listItemView.findViewById(R.id.description);
TextView ageTextView = listItemView.findViewById(R.id.age);
ImageView imageView = listItemView.findViewById(R.id.image);
String firstName = currentList.getFirstName();
String lastName = currentList.getLastName();
String name = firstName + " " + lastName;
nameTextView.setText(name);
descriptionTextView.setText(currentList.getDescription());
ageTextView.setText(currentList.getAge());
return listItemView;
}
List<Informatin> informatins;
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Informatin> results = new ArrayList<>();
if (informatins == null)
informatins = arrayList;
if (constraint != null) {
if (informatins != null && informatins.size() > 0) {
for (final Informatin g : informatins) {
if (g.getFirstName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
arrayList = (ArrayList<Informatin>) results.values;
notifyDataSetChanged();
}
};
}
}
this is the picture of the problem
this is a screenshot for the problem from the device
Add this to your listadapter code:
Context context;
public listAdapter(Context context, List<Informatin> informatins) {
super();
this.context=context;
arraylist=informatins;
}
ArrayList<Informatin> informatins;
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Informatin> results = new ArrayList<Informatin>();
if (informatins == null)
informatins = arraylist;
if (constraint != null) {
if (informatins != null && informatins.size() > 0) {
for (final Information g : informatins) {
if (g.getName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
arraylist = (ArrayList<Informatin>) results.values;
notifyDataSetChanged();
}
};
}
Add this in your onquerytextchaged:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);//replace adapter with listview name which in your case is listview as informed by you in comment
return true;
}
});
return true;
Modified MainActivity onCreate() code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItem = new ArrayList<>();
listView = findViewById(R.id.list);
LoaderManager.getInstance(this).initLoader(LIST_APP_LOADER, null, this);
mInformation = new ArrayList<>();
adapter = new listAdapter(this, mInformation);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true)//add this line
}
I'm using SearchView to filter music items. But when I search for an item the title of the song updates but not the actual song. Originally I had listSongs = musicFiles which worked but didn't update the items only the title. Then I changed it to listSongs = mFiles, which throws a nullPointerException.
MusicAdapter
public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.MyVieHolder> {
private Context mContext;
static ArrayList<MusicFiles> mFiles = new ArrayList<>();
MusicAdapter(Context mContext, ArrayList<MusicFiles> mFiles){
MusicAdapter.mFiles = mFiles;
this.mContext = mContext;
}
#NonNull
#Override
public MyVieHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.music_items, parent, false);
return new MyVieHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyVieHolder holder, final int position) {
holder.file_name.setText(mFiles.get(position).getTitle());
byte[] image = getAlbumArt(mFiles.get(position).getPath());
if (image != null){
Glide.with(mContext).asBitmap()
.load(image)
.into(holder.album_art);
}
else{
Glide.with(mContext).asBitmap()
.load(R.drawable.something_3)
.into(holder.album_art);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, MainActivity.class);
intent.putExtra("position" , position);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return mFiles.size();
}
public class MyVieHolder extends RecyclerView.ViewHolder{
TextView file_name;
ImageView album_art;
public MyVieHolder(#NonNull View itemView) {
super(itemView);
file_name = itemView.findViewById(R.id.music_file_name);
album_art = itemView.findViewById(R.id.music_img);
}
}
private byte[] getAlbumArt(String uri){
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(uri);
byte[] art = retriever.getEmbeddedPicture();
retriever.release();
return art;
}
public void updateList(ArrayList<MusicFiles> musicFilesArrayList){
mFiles = new ArrayList<>();
mFiles.addAll(musicFilesArrayList);
notifyDataSetChanged();
}
MainActivity
This is the code I'm having trouble with
public void getInentMethod() {
position = getIntent().getIntExtra("position", 0);
listSongs = mFiles; //bug occurs when listSongs = mFiles
if (listSongs != null) {
playBtn.setBackgroundResource(R.drawable.ic_pause);
uri = Uri.parse(listSongs.get(position).getPath());
}
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
mediaPlayer.start();
}
else {
mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
playBtn.setBackgroundResource(R.drawable.ic_play);
mediaPlayer.stop();
}
seekBar.setMax(mediaPlayer.getDuration()/1000);
metaData(uri);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
MenuItem menuItem = menu.findItem(R.id.search_option);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onQueryTextSubmit (String query){
return false;
}
#Override
public boolean onQueryTextChange (String newText){
String userInput = newText.toLowerCase();
ArrayList<MusicFiles> musicFilesArrayList = new ArrayList<>();
for (MusicFiles mFiles : musicFiles){
if(mFiles.getTitle().toLowerCase().contains(userInput)){
musicFilesArrayList.add(mFiles);
}
}
musicAdapter.updateList(musicFilesArrayList);
return true;
}
When listSongs = mFiles I receive a nullPointerException, uri param cannot be null. I don't know what's causing this?
I'm also receiving an IndexOutOfBoundsException.
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0.
Any help will be appreciated.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_bar"
android:title="search"
android:icon="#drawable/ic_baseline_search_24"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.widget.SearchView" />
</menu>
Inside onCreateOptionsMenu
getMenuInflater().inflate(R.menu.main_menu,menu);
MenuItem searchItem=menu.findItem(R.id.search_bar);
MenuItem settingsItem=menu.findItem(R.id.action_settings);
SearchView searchView= (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//do filter here
adapter.getFilter().filter(newText);
return false;
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getItemId()==R.id.search_bar) return true;
return super.onOptionsItemSelected(item);
}
In the Adapter class, create two arraylists
class Adapter implements Filterable{
private List<TotalItems> totalItems; //original
private List<TotalItems> totalItemsAll; //copy
Context context;
public Adapter(ArrayList<TotalItems> totalItems,Context context){
this.totalItems=totalItems;
this.context=context;
this.totalItemsAll=new ArrayList<>(totalItems);
}
#Override
public Filter getFilter() {
return filter;
}
private Filter filter=new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
//TotalItems is my model class
List<TotalItems> filteredList=new ArrayList<>();
if (charSequence==null||charSequence.length()==0)
filteredList.addAll(totalItemsAll);
else {
String filterPattern=charSequence.toString().toLowerCase().trim();
for (TotalItems productList:totalItemsAll){
if (productList.getiName().toLowerCase().contains(filterPattern)){
filteredList.add(productList);
}else Toast.makeText(context,"No results found",Toast.LENGTH_LONG).show();
}
}
FilterResults filterResults=new FilterResults();
filterResults.values=filteredList;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
totalItems.clear();
totalItems.addAll((List)filterResults.values);
notifyDataSetChanged();
}
};
}
Let me know if this works for you
I m currently working on a android apps which fetch data from parse server. I wanted to filter my recyclerView by using searchview. But it shows me nothing while search. It gives me error in this line*for (ParseObject parseObject : mRooms){ * Please help me to edit my code regarding this issues.
roomcardrecyclerviewadapter
private List<ParseObject> mRooms = new ArrayList<>();
private ArrayList<ParseObject> filterlist;
private ParseObject room;
private String mSection;
private Context context;
public RoomCardRecyclerViewAdapter(){
super(DIFF_CALLBACK);
}
public static final DiffUtil.ItemCallback<ParseObject> DIFF_CALLBACK = new
DiffUtil.ItemCallback<ParseObject>() {
#Override
public boolean areItemsTheSame(#NonNull ParseObject oldItem, #NonNull ParseObject newItem) {
return oldItem.getObjectId() == newItem.getObjectId();
}
#Override
public boolean areContentsTheSame(#NonNull ParseObject oldItem, #NonNull ParseObject newItem) {
return (oldItem.getUpdatedAt().equals(newItem.getUpdatedAt()) &&
oldItem.getCreatedAt().equals(newItem.getCreatedAt()));
}
};
public RoomCardRecyclerViewAdapter(String section) {
this();
this.mSection = section;
}
public RoomCardRecyclerViewAdapter(Context context, List<ParseObject>arrayList) {
this();
this.context = context;
mRooms = arrayList;
filterlist = (ArrayList<ParseObject>) arrayList;
}
public class RoomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected ImageView mRoomImage;
protected TextView mRoomPrice;
protected TextView mInclusiveOrNot;
protected TextView mPropertyType;
protected TextView mNumOfBeds;
protected TextView mNumOfBaths;
protected TextView mRoomLocation;
private Context context;
public RoomViewHolder(Context context, View itemView) {
super(itemView);
mRoomImage = itemView.findViewById(R.id.room_image);
mRoomPrice = itemView.findViewById(R.id.price_label);
mInclusiveOrNot = itemView.findViewById(R.id.incl_excl_label);
mPropertyType = itemView.findViewById(R.id.propertyType_label);
mNumOfBeds = itemView.findViewById(R.id.num_beds_label);
mNumOfBaths = itemView.findViewById(R.id.details_num_baths_label);
mRoomLocation = itemView.findViewById(R.id.location_label);
this.context = context;
//set onclick listener
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Log.i("Click event: ", "My room has been clicked.");
int pos = getAdapterPosition();
Intent intent;
ParseObject room = getCurrentList().get(pos);
//create the ParseObject proxy
ParseProxyObject roomProxy = new ParseProxyObject(room);
Toast.makeText(context, room.getString("roomSuburb"), Toast.LENGTH_LONG).show();
//fork to corresponding activity
if(mSection != null) {
Log.i("mSection text: ", "mSection text is: " + mSection);
if (mSection.equals("My Rooms")) {
//start my rooms detail activity
Log.i("My room: ", "Room selected " + roomProxy.getObjectId());
intent = new Intent(context, MyRoomDetailActivity.class);
//add the room to the intent
intent.putExtra("currentSelectedRoomObject", room);
Log.i("Selected room", "Put Extra, " + room);
intent.putExtra("roomObject", roomProxy);
context.startActivity(intent);
}
}else {
Log.i("My room:", "RoomDetailActivity loaded for MyRoomDetail Activity instead");
intent = new Intent(context, RoomDetailActivity.class);
//add the proxy to the intent
intent.putExtra("roomObject", roomProxy);
context.startActivity(intent);
}
}
}
#Override
public RoomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating the viewholder with the appropriate views
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.room_cardview, parent,
false);
return new RoomViewHolder(parent.getContext(), view);
}
#Override
public void onBindViewHolder(#NonNull RoomViewHolder holder, int position) {
room = getItem(position);
holder.mRoomLocation.setText(room.getString("roomSuburb"));
holder.mRoomPrice.setText(Integer.toString(room.getInt("roomMonthlyRent")));
holder.mInclusiveOrNot.setText(room.getString("roomRentInclusiveOfBills"));
holder.mPropertyType.setText(room.getString("roomPropertyType"));
holder.mNumOfBeds.setText(Integer.toString(room.getInt("roomBedrooms")));
holder.mNumOfBaths.setText(Integer.toString(room.getInt("roomBathrooms")));
#Override
public Filter getFilter(){
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()){
room = (ParseObject) mRooms;
}else {
List<ParseObject> filteredList = new ArrayList<>();
for (ParseObject parseObject : mRooms){
if (parseObject.getString("roomSuburb").toLowerCase().contains(charString.toLowerCase())){
filteredList.add(parseObject);
}
}
room = (ParseObject) filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = room;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
room = (ParseObject) filterResults.values;
notifyDataSetChanged();
}
};
}
mainActivity
mHomeSectionsPagerAdapter = new
HomeSectionsPagerAdapter(getSupportFragmentManager());
roomCardRecyclerViewAdapter = new RoomCardRecyclerViewAdapter(this,
mRooms);
mRooms = new ArrayList<>();
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById(R.id.container);
mViewPager.setAdapter(mHomeSectionsPagerAdapter);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
final ParseQuery<ParseUser> query = ParseUser.getQuery();
//get the search view and set the searchable configuration
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
MenuItem item = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
//assumes the current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo
(getComponentName());
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
roomCardRecyclerViewAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
roomCardRecyclerViewAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
#Override
public boolean onSearchRequested() {
//pauseSomeStuff();
//roomCardRecyclerViewAdapter.getFilter().filter(query);
return super.onSearchRequested();
}
I think problem is here:
You haven't shared first lines of MainActivity where you declare your variables. So, I assume that you have some code like this:
List mRooms;
Then I assume that, before assigning value to this mRooms you have passed it to RoomCardRecyclerViewAdapter:
roomCardRecyclerViewAdapter = new RoomCardRecyclerViewAdapter(this, mRooms);
mRooms = new ArrayList<>();
Then you have assigned value to mRooms. That's why you get NPE. You can solve this problem just like this:
mRooms = new ArrayList<>();
roomCardRecyclerViewAdapter = new RoomCardRecyclerViewAdapter(this, mRooms);
I am having trouble with my filtering logic in my recyclerview. Whenever I type into the searchview I have the recyclerview remains the same it does not change to no matter what I type in the searchview.
Here is my RecyclerViewAdapter:
public class UniversitiesAdapter extends RecyclerView.Adapter<UniversitiesAdapter.UniversitiesViewHolder> implements Filterable{
private Context mCtx;
public ArrayList<Universities> universitiesList;
public ArrayList<Universities> universityFilter;
public UniversitiesAdapter(Context mCtx, ArrayList<Universities> universitiesList){
this.mCtx = mCtx;
this.universitiesList = universitiesList;
this.universityFilter = universitiesList;
}
#Override
public UniversitiesViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.university_cardview_rows,null);
return new UniversitiesViewHolder(view);
}
#Override
public void onBindViewHolder(UniversitiesViewHolder holder, int position){
UniversitiesViewHolder.currentUniversity = universityFilter.get(position);
Universities universities = universityFilter.get(position);
//loading the image
Glide.with(mCtx)
.load(universities.getUniversityLogo())
.into(holder.uniLogo);
holder.uniLocation.setText(universities.getUniveristyLocation());
holder.uniName = universities.getUniversityName();
holder.uniID = universities.getUniversityId();
holder.universityLongitude = universities.getUniversityLongitude();
holder.universityLatitude = universities.getUniversityLatitude();
holder.dormBldgNumber.setText(String.valueOf(universities.getDormbuildingsNumber()));
//TODO add holder for number of dorm buildings
}
#Override
public int getItemCount(){
return universityFilter.size();
}
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
universityFilter = universitiesList;
Log.d("hello",String.valueOf(universityFilter));
} else {
ArrayList<Universities> filteredList = new ArrayList<>();
for (Universities universities : universitiesList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (universities.getUniversityName().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(universities);
Log.d("HELP", String.valueOf(universities));
}
}
universityFilter = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = universityFilter;
Log.d("hell", String.valueOf(filterResults));
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
universityFilter = (ArrayList<Universities>) filterResults.values;
notifyDataSetChanged();
}
};
}
public static class UniversitiesViewHolder extends RecyclerView.ViewHolder{
public String uniName;
public int uniID;
public double universityLongitude, universityLatitude;
TextView uniLocation, dormBldgNumber;
ImageView uniLogo;
public View view;
public static Universities currentUniversity;
public UniversitiesViewHolder(View itemView){
super(itemView);
view = itemView;
view.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View itemView){
Intent intent = new Intent(itemView.getContext(),DormBuildings.class);
intent.putExtra("uniID",uniID);
intent.putExtra("uniName",uniName);
intent.putExtra("universityLongitude", universityLongitude);
intent.putExtra("universityLatitude", universityLatitude);
itemView.getContext().startActivity(intent);
}
});
uniLocation = itemView.findViewById(R.id.uniLocation);
dormBldgNumber= itemView.findViewById(R.id.dormBldgNumber);
uniLogo = itemView.findViewById(R.id.uniLogo);
}
}
}
Here is my main activity:
public class UniversityPick extends AppCompatActivity {
//TODO FIX SEARCH AND DORMBUILDING NUMBER
private static final String URL_PRODUCTS = "http://XXXXX/XXXXX.php";
ArrayList<Universities> universitiesList;
RecyclerView recyclerView;
SearchView searchView;
SwipeRefreshLayout unirefresh;
DrawerLayout mDrawerLayout;
private UniversitiesAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_university_pick);
searchView = (SearchView) findViewById(R.id.searchView);
searchView.setIconifiedByDefault(false);
searchView.clearFocus();
//University list
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
universitiesList = new ArrayList<>();
loadUniversities();
adapter = new UniversitiesAdapter(UniversityPick.this,universitiesList);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
unirefresh = (SwipeRefreshLayout) findViewById(R.id.unirefresh);
unirefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
loadUniversities();
unirefresh.setRefreshing(false);
}
});
}
private void loadUniversities(){
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for (int i = 0; i<array.length(); i++){
JSONObject universities = array.getJSONObject(i);
universitiesList.add(new Universities(
universities.getInt("universityId"),
universities.getString("universityName"),
universities.getString("universityLocation"),
universities.getString("universityLogo"),
universities.getDouble("universityLongitude"),
universities.getDouble("universityLatitude"),
universities.getInt("dormbuildingsNumber")
));
}
UniversitiesAdapter adapter = new UniversitiesAdapter(UniversityPick.this,universitiesList);
recyclerView.setAdapter(adapter);
} catch(JSONException e) {
e.printStackTrace()
;
}
}
},
new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
}
});
Volley.newRequestQueue(this).add(stringRequest);
}
There is no error displayed in the logcat and no unusual activity in it either.
Your both lists pointing to same list. It should be as .
public UniversitiesAdapter(Context mCtx, ArrayList<Universities> universitiesList){
this.mCtx = mCtx;
this.universitiesList = universitiesList;
universityFilter=new ArrayList<>();
universityFilter.addAll(universitiesList);
}
And in filter :-
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
universityFilter.clear();
if (charString.isEmpty()) {
universityFilter.addAll();
Log.d("hello",String.valueOf(universityFilter));
} else {
for (Universities universities : universitiesList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (universities.getUniversityName().toLowerCase().contains(charString.toLowerCase())) {
universityFilter.add(universities);
Log.d("HELP", String.valueOf(universities));
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = universityFilter;
Log.d("hell", String.valueOf(filterResults));
return filterResults;
}
I'm currently developing an Android application which can either load information from my website or use the information stored in an SQLite database. I'm trying to implement a search feature into it but I ran into a problem.
The data in my recyclerview doesn't update when a search is made. I've looked it up on Google many times but I still haven't found a solution. Is there a bright person here willing to help me?
MainActivity.java
public class MainActivity extends AppCompatActivity implements DienstAdapter.ListItemClickListener, NavigationView.OnNavigationItemSelectedListener {
private static final String DIENST_URL = "https://mywebsite.com/apps/test/dienstenapi.php";
private RecyclerView recyclerView;
private DienstAdapter adapter;
private List<Dienst> dienstList;
private SQLiteDatabase mDb;
private Cursor cursor;
private SharedPreferences sharedPreferences;
private NavigationView navigationView;
private View headerView;
private TextView gebruikersnaam;
private Resources res;
private SwipeRefreshLayout swiper;
private static Toast mToast = null;
private SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = this.getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(R.string.app_name);
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
headerView = navigationView.getHeaderView(0);
gebruikersnaam = headerView.findViewById(R.id.gebruikersnaam);
res = getResources();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
setupPreferenceGebruikersnaam();
swiper = findViewById(R.id.swiper);
swiper.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
dienstList.clear();
loadDiensten();
}
});
dienstList = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
DienstDbHelper dbHelper = new DienstDbHelper(this);
mDb = dbHelper.getWritableDatabase();
loadDiensten();
}
#Override
protected void onDestroy() {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
super.onDestroy();
}
SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("example_text")){
setupPreferenceGebruikersnaam();
}
}
};
private void loadDienstenOnline() {
String moetInfoOphalen = sharedPreferences.getString("example_list", "1");
if (moetInfoOphalen.equals("1") || moetInfoOphalen.equals("0")) {
StringRequest stringRequest = new StringRequest(Request.Method.GET, DIENST_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray diensten = new JSONArray(response);
for (int i = 0; i < diensten.length(); i++) {
JSONObject dienstObject = diensten.getJSONObject(i);
int id = dienstObject.getInt("id");
String naam = dienstObject.getString("naam");
String beschrijving = dienstObject.getString("beschrijving");
String categorie = dienstObject.getString("categorie");
double prijs = dienstObject.getDouble("prijs");
String afbeelding = dienstObject.getString("afbeelding");
dienstList.add(new Dienst(id, naam, beschrijving, categorie, prijs, afbeelding));
}
voegDienstenToeAanDb(mDb, dienstList);
adapter = new DienstAdapter(MainActivity.this, dienstList, MainActivity.this);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
Volley.newRequestQueue(this).add(stringRequest);
} else {
loadDienstenOffline();
Toast.makeText(MainActivity.this, R.string.instelling_blokkeert_ophalen_diensten, Toast.LENGTH_LONG).show();
}
}
private void loadDienstenOffline() {
cursor = getAlleDiensten();
if ((cursor != null) && (cursor.getCount() > 0)) {
omzettenCursorNaarDiensten(cursor);
adapter = new DienstAdapter(MainActivity.this, dienstList, MainActivity.this);
recyclerView.setAdapter(adapter);
} else {
dienstList.add(new Dienst(0, "", "", "", 0, ""));
adapter = new DienstAdapter(MainActivity.this, dienstList, MainActivity.this);
recyclerView.setAdapter(adapter);
recyclerView.setVisibility(View.GONE);
if (mToast != null)
mToast.cancel();
mToast.makeText(this, R.string.internet_werkt_niet, Toast.LENGTH_LONG).show();
}
}
private boolean internetIsBeschikbaar() {
ConnectivityManager cm =(ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
assert cm != null;
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
private void loadDiensten() {
recyclerView.setVisibility(View.VISIBLE);
if (internetIsBeschikbaar()) {
loadDienstenOnline();
} else {
loadDienstenOffline();
}
swiper.setRefreshing(false);
}
#Override
public void onListItemClick(String url, String naam) {
Intent intent = new Intent(MainActivity.this, DienstActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, url);
intent.putExtra("DienstNaam", naam);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
inflater.inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager != null ? searchManager.getSearchableInfo(getComponentName()) : null);
searchView.setMaxWidth(Integer.MAX_VALUE);
// listening to search query text change
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// filter recycler view when query submitted
adapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
// filter recycler view when text is changed
adapter.getFilter().filter(query);
return false;
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent startSettingsActivity = new Intent(this, SettingsActivity.class);
startActivity(startSettingsActivity);
return true;
} else if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
return;
}
if (!searchView.isIconified()) {
searchView.setIconified(true);
return;
}
super.onBackPressed();
}
private static void voegDienstenToeAanDb(SQLiteDatabase db, List<Dienst> diensten){
if(db == null){
return;
}
List<ContentValues> list = new ArrayList<>();
ContentValues cv;
for (Dienst d : diensten) {
cv = new ContentValues();
cv.put(DienstContract.DienstEntry.COLUMN_NAAM, d.getNaam());
cv.put(DienstContract.DienstEntry.COLUMN_BESCHRIJVING, d.getBeschrijving());
cv.put(DienstContract.DienstEntry.COLUMN_CATEGORIE, d.getCategorie());
cv.put(DienstContract.DienstEntry.COLUMN_PRIJS, d.getPrice());
cv.put(DienstContract.DienstEntry.COLUMN_AFBEELDING, d.getAfbeelding());
list.add(cv);
}
try
{
db.beginTransaction();
//clear the table first
db.delete (DienstContract.DienstEntry.TABLE_NAME,null,null);
//go through the list and add one by one
for(ContentValues c : list){
db.insert(DienstContract.DienstEntry.TABLE_NAME, null, c);
}
db.setTransactionSuccessful();
}
catch (SQLException e) {
//too bad :(
}
finally
{
db.endTransaction();
}
}
private Cursor getAlleDiensten() {
return mDb.query(
DienstContract.DienstEntry.TABLE_NAME,
null,
null,
null,
null,
null,
DienstContract.DienstEntry._ID
);
}
private void omzettenCursorNaarDiensten(Cursor c) {
int teller = 0, id;
String naam, beschrijving, categorie, afbeelding;
double prijs;
while (c.moveToPosition(teller)) {
id = Integer.parseInt(c.getString(c.getColumnIndex(DienstContract.DienstEntry._ID)));
naam = c.getString(c.getColumnIndex(DienstContract.DienstEntry.COLUMN_NAAM));
beschrijving = c.getString(c.getColumnIndex(DienstContract.DienstEntry.COLUMN_BESCHRIJVING));
categorie = c.getString(c.getColumnIndex(DienstContract.DienstEntry.COLUMN_CATEGORIE));
prijs = c.getDouble(c.getColumnIndex(DienstContract.DienstEntry.COLUMN_PRIJS));
afbeelding = c.getString(c.getColumnIndex(DienstContract.DienstEntry.COLUMN_AFBEELDING));
dienstList.add(new Dienst(id, naam, beschrijving, categorie, prijs, afbeelding));
teller++;
}
}
private void setupPreferenceGebruikersnaam() {
String text = String.format(res.getString(R.string.welkomsbericht), sharedPreferences.getString("example_text", "gebruiker"));
gebruikersnaam.setText(text);
}
}
DienstAdapter.java
public class DienstAdapter extends RecyclerView.Adapter<DienstAdapter.DienstViewHolder> implements Filterable {
private Context mContext;
private List<Dienst> dienstList;
private List<Dienst> dienstListFiltered;
private final ListItemClickListener mOnClickListener;
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
dienstListFiltered = dienstList;
} else {
List<Dienst> filteredList = new ArrayList<>();
for (Dienst row : dienstList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNaam().toLowerCase().contains(charString.toLowerCase()) || row.getBeschrijving().contains(charSequence)) {
filteredList.add(row);
}
}
dienstListFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = dienstListFiltered;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
dienstListFiltered = (ArrayList<Dienst>) filterResults.values;
notifyDataSetChanged();
}
};
}
public interface ListItemClickListener {
void onListItemClick(String url, String naam);
}
DienstAdapter(Context mContext, List<Dienst> dienstList, ListItemClickListener listener) {
this.mContext = mContext;
this.dienstList = dienstList;
this.dienstListFiltered = dienstList;
mOnClickListener = listener;
}
#Override
public DienstViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.list_layout, null);
return new DienstViewHolder(view);
}
#Override
public void onBindViewHolder(DienstViewHolder holder, int position) {
Dienst dienst = dienstList.get(position);
holder.tv_naam.setText(dienst.getNaam());
holder.tv_beschrijving.setText(dienst.getBeschrijving());
holder.tv_categorie.setText(String.valueOf(dienst.getCategorie()));
holder.tv_prijs.setText(String.valueOf(dienst.getPrice()));
if (dienst.getPrice() != 0)
holder.tv_prijs.setText(String.format("€ %.2f", dienst.getPrice()));
else
holder.tv_prijs.setText("");
Glide.with(mContext).load(dienst.getAfbeelding()).into(holder.iv_afbeelding);
}
#Override
public int getItemCount() {
return dienstList.size();
}
public class DienstViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView iv_afbeelding;
TextView tv_naam, tv_beschrijving, tv_categorie, tv_prijs;
DienstViewHolder(View itemView) {
super(itemView);
iv_afbeelding = itemView.findViewById(R.id.iv_afbeelding);
tv_naam = itemView.findViewById(R.id.tv_naam);
tv_beschrijving = itemView.findViewById(R.id.tv_beschrijving);
tv_categorie = itemView.findViewById(R.id.tv_categorie);
tv_prijs = itemView.findViewById(R.id.tv_prijs);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int clickedPosition = getAdapterPosition();
String url = dienstList.get(clickedPosition).getAfbeelding().replace(".png", ".html");
String naam = dienstList.get(clickedPosition).getNaam();
mOnClickListener.onListItemClick(url, naam);
}
}
}
Firstly, getItemCount() and onBindViewHolder() should use the filtered list dienstListFiltered.
And You forgot to update the count of the FilterResults.
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
// Filter data ...
// ...
FilterResults filterResults = new FilterResults();
filterResults.values = dienstListFiltered;
filterResults.count = dienstListFiltered.size();
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
dienstListFiltered = (ArrayList<Dienst>) filterResults.values;
notifyDataSetChanged();
}
};
}
See: Custom Listview Adapter with filter Android
Can you add
notifyDataSetChanged();
immediately after
filteredList.add(row);
?