To better address your issue, you need to completely rethink the structure of your project. So, it is not very efficient to save favorites to a separate list, since finding out if an item is selected or not is an unreasonable amount of a resource. Also, making two separate buttons to add to favorites, or delete from it - hiding one and showing the other, is also very inefficient.
I offer a more optimal solution.
First, get rid of a separate list of favorites by adding a state flag (boolean) to the model of the main list item - in the selected item or not. This will greatly reduce the resource consumption and greatly simplify the logic.
Secondly, instead of two buttons hiding in turn, use one in which to change the text depending on the current state of the element.
I propose a simple code that implements these ideas, based on the ObjectBox database for example (how to connect to the project ). If you wish, you can easily modify it to fit your data storage structure.
For example, we have a list of some stations (for simplicity, only the fields with the names of stations are filled in), and below the list is a selector button, a successive click on which switches the view to all stations, or to only selected stations. At each station point there is a button, clicking on which either adds the station to the favorites (if it is not there), or removes it from the favorites (if it was there). Comments in the code should clarify the main points of the logic of work.
First, create a data model for ObjectBox (standard POJO with getters / setters):
@Entity public class Station { @Id long id; String Name; String url; boolean favorite; public Station() { } public Station(String name) { Name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public boolean isFavorite() { return favorite; } public void setFavorite(boolean favorite) { this.favorite = favorite; } }
note: the model class for ObjectBox must contain a field annotated with @id and a constructor with no arguments.
Activi class where all the magic happens:
public class MainActivity extends AppCompatActivity { String [] stations = {"Station1","Station2","Station3","Station4","Station5","Station6","Station7","Station8","Station9"}; BoxStore boxStore; Box<Station> stationsBox; StationAdapter adapter; Button buttonSelect; boolean onlyFavorite; // ΠΏΠΎΠΊΠ°Π·ΡΠ²Π°ΡΡ Π²Π΅ΡΡ ΡΠΏΠΈΡΠΎΠΊ ΠΈΠ»ΠΈ ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠ΅ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); boxStore = MyObjectBox.builder().androidContext(this).build(); stationsBox = boxStore.boxFor(Station.class); if (stationsBox.getAll().isEmpty()) { // Π΅ΡΠ»ΠΈ Π±Π°Π·Π° Π½Π΅ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½Π°, ΡΠΎ Π·Π°ΠΏΠΎΠ»Π½ΡΠ΅ΠΌ Π΄Π°Π½Π½ΡΠΌΠΈ for(String station:stations){ stationsBox.put(new Station(station)); } } buttonSelect = findViewById(R.id.buttonSelect); RecyclerView rv = findViewById(R.id.list); buttonSelect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ΠΏΠ΅ΡΠ΅ΠΊΠ»ΡΡΠ°Π΅ΠΌ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π²ΡΠ΅Π³ΠΎ ΡΠΏΠΈΡΠΊΠ° ΠΈΠ»ΠΈ ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠ³ΠΎ onlyFavorite = !onlyFavorite; String textButton = onlyFavorite? "All Station": "Only favorite station"; buttonSelect.setText(textButton); //ΠΎΠ±Π½ΠΎΠ²Π»ΡΠ΅ΠΌ ΡΠΏΠΈΡΠΎΠΊ getDataset(onlyFavorite); } }); rv.setHasFixedSize(true); rv.setLayoutManager(new LinearLayoutManager(this)); ArrayList<Station> dataset = getDataset(onlyFavorite); adapter = new StationAdapter(dataset); rv.setAdapter(adapter); } private ArrayList<Station> getDataset(boolean onlyFavorite) { // Π΄Π΅Π»Π°Π΅ΠΌ Π²ΡΠ±ΠΎΡΠΊΡ Π² ΠΠ. ΠΠΎΠΊΠ°Π·ΡΠ²Π°ΡΡ Π²ΡΠ΅ ΡΡΠ°Π½ΡΠΈΠΈ ΠΈΠ»ΠΈ ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ·Π±ΡΠ°Π½Π½ΡΠ΅ ArrayList<Station> dataset = (ArrayList<Station>) (onlyFavorite? stationsBox.query().equal(Station_.favorite, true).build().find(): stationsBox.getAll()); //ΠΎΠ±Π½ΠΎΠ²Π»ΡΠ΅ΠΌ ΡΠΏΠΈΡΠΎΠΊ if (adapter != null) adapter.notifyList(dataset); return dataset; } public void onClickFavorite(View view){ // Π·Π΄Π΅ΡΡ ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅ΠΌ ΠΊΠ»ΠΈΠΊ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅ ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠ³ΠΎ Π² Π°ΠΉΡΠ΅ΠΌΠ΅ ΡΠΏΠΈΡΠΊΠ° // ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΏΠΎ Π°ΡΡΠΈΠ±ΡΡΡ android:onClick xml-ΡΠ°Π·ΠΌΠ΅ΡΠΊΠΈ Π°ΠΉΡΠ΅ΠΌΠ° // ΠΠΎΠ»ΡΡΠ°Π΅ΠΌ ID ΡΡΠ°Π½ΡΠΈΠΈ, Π½Π° ΠΊΠΎΡΠΎΡΠΎΠΉ ΠΊΠ»ΠΈΠΊΠ½ΡΠ»ΠΈ ΠΊΠ½ΠΎΠΏΠΊΡ ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠ³ΠΎ long id = (long) view.getTag(); // ΠΈΠ½Π²Π΅ΡΡΠΈΡΡΠ΅ΠΌ ΠΎΡΠΌΠ΅ΡΠΊΡ ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠ³ΠΎ Station station = stationsBox.get(id); station.setFavorite(!station.isFavorite()); stationsBox.put(station); //ΠΎΠ±Π½ΠΎΠ²Π»ΡΠ΅ΠΌ ΡΠΏΠΈΡΠΎΠΊ getDataset(onlyFavorite); } @Override protected void onStop() { super.onStop(); boxStore.close(); } }
note: when using the ObjectBox, in order for the MyObjectBox class not to show an error, you need to crash the project (Build -> rebuild project), since this is an auto-generated class and to create it you need to build a project.
Adapter for listing:
public class StationAdapter extends RecyclerView.Adapter<StationAdapter.ViewHolder>{ ArrayList<Station> stations; public StationAdapter(ArrayList<Station> stations) { this.stations = stations; } @Override public StationAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false)); } @Override public void onBindViewHolder(StationAdapter.ViewHolder holder, int position) { Station station = stations.get(position); holder.name.setText(station.getName()); // ΠΠ°Π΄ΠΏΠΈΡΡ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅ ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠ³ΠΎ // ΠΡΠ»ΠΈ Π½Π΅ Π² ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠΌ, ΡΠΎ "Π΄ΠΎΠ±Π°Π²ΠΈΡΡ". ΠΡΠ»ΠΈ Π² ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠΌ, ΡΠΎ "ΡΠ±ΡΠ°ΡΡ" String buttonText = station.isFavorite()? "Delete favorite": "Add favorite"; holder.buttonFavorite.setText(buttonText); // ΠΎΡΠΏΡΠ°Π²Π»ΡΠ΅ΠΌ Π² Π°ΠΊΡΠΈΠ²ΠΈΡΠΈ ID ΡΡΠ°Π½ΡΠΈΠΈ, Π½Π° ΠΊΠΎΡΠΎΡΠΎΠΉ Π½Π°ΠΆΠ°Π»ΠΈ ΠΊΠ½ΠΎΠΏΠΊΡ ΠΈΠ·Π±ΡΠ°Π½Π½ΠΎΠ³ΠΎ holder.buttonFavorite.setTag(station.getId()); } @Override public int getItemCount() { return stations.size(); } public void notifyList(ArrayList<Station> stations){ this.stations = stations; notifyDataSetChanged(); } public class ViewHolder extends RecyclerView.ViewHolder { final TextView name; final Button buttonFavorite; ViewHolder(View view){ super(view); name = view.findViewById(R.id.name); buttonFavorite = view.findViewById(R.id.buttonFavorite); } } }
note: to communicate with the activation at the press of a button, the listener assigned in the xml markup of the item through the android:onClick attribute android:onClick - this method works only in activation and does not work in fragments. Other ways to handle click in RecyclerView
Item Marking:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp"> <TextView android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:paddingLeft="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/buttonFavorite" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="Station" /> <Button android:id="@+id/buttonFavorite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClickFavorite" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="Add favorite" /> </android.support.constraint.ConstraintLayout>
All project on github