I implemented an Android application that must display the content of a list in an Activity, using ViewPager2. See an extract of the code below:
public class MainActivity extends AppCompatActivity {
ViewPager2 viewPager2;
DataPageViewAdapter dataPageViewAdapter;
private Button btnFind;
private Control ctrl;
private List<Movie> movies;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager2 = findViewById(R.id.view_pager_2);
ctrl = new Control();
movies = ctrl.loadAllMovies();
btnFind = findViewById(R.id.btn_find);
btnFind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!movies.isEmpty()) {
dataPageViewAdapter = new DataPageViewAdapter(getApplicationContext(), movies);
dataPageViewAdapter.notifyDataSetChanged();
viewPager2.setAdapter(dataPageViewAdapter);
}else{
Toast.makeText(this, "No movies loaded yet!", Toast.LENGTH_LONG).show();
}
}
});
}
}
There is a Movie class with the data stored and a Control class that returns a list of Movies. The application fulfills its purpose, but I have to press the menu action twice so that the information in the Activity is observed. How can I make the content appear at once when pressing the button? Thanks in advance!