My RecyclerView is getting a filtered position after the list is filtered using the SearchView. This means that when the list item is clicked the information for the wrong item is displayed. I'm looking for a way to keep the original list position after filtering the list. Below is code for the RecyclerView Adapter and RecyclerView activity. Thanks in advance.
public class BrowseAdverts extends AppCompatActivity implements SearchView.OnQueryTextListener, AdvertsAdapter.OnAdvertListener, CountySheet.BottomSheetListener {
//Instance of the database helper class
private DatabaseHelper myDb;
private AdvertsAdapter mAdapter;
private List<Advert> advertsList = new ArrayList<>();
private CoordinatorLayout coordinatorLayout;
private TextView noAdvertsView;
private RecyclerView recyclerView;
//https://material.io/develop/android/components/bottom-app-bar/
private BottomAppBar bottomAppBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
//https://stackoverflow.com/questions/39356604/how-to-switch-theme-for-an-activity
setTheme(R.style.AppThemeToolbar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse_adverts);
coordinatorLayout = findViewById(R.id.coordinator_layout_browse);
recyclerView = findViewById(R.id.rvBrowseAdverts);
noAdvertsView = findViewById(R.id.tvNoAdverts);
noAdvertsView.setText("No Adverts");
//https://material.io/develop/android/components/bottom-app-bar/
final FloatingActionButton floatingActionButton =
(FloatingActionButton) findViewById(R.id.floating_action_button_browse);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//https://codinginflow.com/tutorials/android/modal-bottom-sheet
CountySheet bottomSheet = new CountySheet();
bottomSheet.show(getSupportFragmentManager(), "countySheet");
}
});
myDb = new DatabaseHelper(this);
//get all adverts from IS4447 SQLite tutorial
advertsList.addAll(myDb.getAllAdverts());
mAdapter = new AdvertsAdapter(this, advertsList, this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
//add a default divider line between items
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(mAdapter);
//https://material.io/develop/android/components/bottom-app-bar/
bottomAppBar = findViewById(R.id.bar_browse);
FloatingActionButton fab = findViewById(R.id.fab_browse);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(BrowseAdverts.this, CreateAdvert.class);
startActivity(intent);
}
});
bottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(BrowseAdverts.this, MyProfile.class);
startActivity(intent);
return true;
}
});
toggleEmptyAdverts();
}
//to show the text view that says no adverts available when adverts table is empty
private void toggleEmptyAdverts() {
//IS4447 Tutorial
if (myDb.getAdvertCount() > 0) {
noAdvertsView.setVisibility(View.GONE);
} else {
noAdvertsView.setVisibility(View.VISIBLE);
}
}
//From search video
// https://www.youtube.com/watch?v=qzbvDJqXeJs
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(this);
//added to add menu to bottom app bar
getMenuInflater().inflate(R.menu.menu_bar_home, menu);
return true;
}
//autogenerated when implement search
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
//From this video bc the first one only had single strings https://www.youtube.com/watch?v=j9_hcfWVkIc
@Override
public boolean onQueryTextChange(String newText) {
String userInput = newText.toLowerCase();
ArrayList<Advert> newList = new ArrayList<>();
for (Advert advert: advertsList) {
String name = advert.getAdvertName().toLowerCase();
if (name.contains(userInput)){
newList.add(advert);
}
mAdapter.setFilter(newList);
}
return true;
}
//has to be implemented when you implement the UserAdapter.Onclick
@Override
public void onAdvertClick(int position) {
//https://www.youtube.com/watch?v=OUZcjZkJrvY
//send parcel to new activity
Intent intent = new Intent (this, AdvertDetail.class);
intent.putExtra("selected_advert" , advertsList.get(position));
startActivity(intent);
}
//https://android-developers.googleblog.com/2009/12/back-and-other-hard-keys-three-stories.html
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent intent = new Intent(BrowseAdverts.this, MainActivity.class);
startActivity(intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
//Must be implemented for bottom sheet listener
@Override
public void onButtonClicked(String text) {
}
////https://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.app_bar_home:
Intent intent = new Intent(BrowseAdverts.this, MainActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
Adapter
public class AdvertsAdapter extends RecyclerView.Adapter<AdvertsAdapter.MyViewHolder> {
private Context context;
private List<Advert> advertList;
private OnAdvertListener mOnAdvertListener;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView advertName, advertCategory, advertId;
OnAdvertListener onAdvertListener;
public MyViewHolder(View view, OnAdvertListener onAdvertListener){
super(view);
advertName = view.findViewById(R.id.tvName);
advertCategory = view.findViewById(R.id.tvCat);
advertId = view.findViewById(R.id.tvId);
this.onAdvertListener = onAdvertListener;
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
onAdvertListener.onAdvertClick(getAdapterPosition());
}
}
public AdvertsAdapter(Context context, List<Advert> advertList, OnAdvertListener onAdvertListener) {
this.context = context;
this.advertList = advertList;
this.mOnAdvertListener = onAdvertListener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.advert_list_row, parent, false);
return new MyViewHolder(itemView, mOnAdvertListener);
}
@Override
public void onBindViewHolder (MyViewHolder holder, int position) {
Advert advert = advertList.get(position);
holder.advertName.setText(advert.getAdvertName());
holder.advertCategory.setText(advert.getAdvertCategory());
holder.advertId.setText(advert.getAdvertId() + "");
}
public int getItemCount() {
return advertList.size();
}
//From this video bc the tutorial one only had singular strings https://www.youtube.com/watch?v=j9_hcfWVkIc
public void setFilter(ArrayList<Advert> newList){
advertList = new ArrayList<>();
advertList.addAll(newList);
notifyDataSetChanged();
}
public interface OnAdvertListener{
void onAdvertClick (int position);
}