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!
CodePudding user response:
You should set the adapter first and then call notifyDataSetChanged()
@Override
public void onClick(View v) {
if(!movies.isEmpty()) {
dataPageViewAdapter = new DataPageViewAdapter(getApplicationContext(), movies);
viewPager2.setAdapter(dataPageViewAdapter);
dataPageViewAdapter.notifyDataSetChanged();
} else {
Toast.makeText(this, "No movies loaded yet!", Toast.LENGTH_LONG).show();
}
}
CodePudding user response:
This is my Adapter and Holder for the ViewPager2
package com.example.test.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.List;
import com.example.test.R;
import com.example.test.models.Configuration.APIVars;
import com.example.test.models.Movies.Movie;
public class DataPageViewAdapter extends RecyclerView.Adapter<ViewHolderA> {
Context context;
List<Movie> myMovies;
LayoutInflater li;
public DataPageViewAdapter(Context context, List<Movie> movies){
this.context = context;
this.myMovies = movies;
li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
public ViewHolderA onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View mv = li.inflate(R.layout.movie_view_detail,parent,false);
return new ViewHolderA(mv);
}
@Override
public void onBindViewHolder(@NonNull ViewHolderA holder, int position) {
holder.bindData(myMovies.get(position));
}
@Override
public int getItemCount() {
return myMovies.size();
}
}
class ViewHolderA extends RecyclerView.ViewHolder{
private final ImageView movie_poster;
private final TextView tv_title;
private final TextView tv_id;
private final TextView tv_overview;
private final TextView tv_genre;
private final TextView tv_release_date;
private final TextView tv_vote_count;
private final TextView tv_vote_avg;
private final View mv;
/*My ViewHolderA Constructor*/
public ViewHolderA(@NonNull View itemView) {
super(itemView);
mv = itemView;
movie_poster = mv.findViewById(R.id.movie_poster);
tv_id = mv.findViewById(R.id.tv_id);
tv_title = mv.findViewById(R.id.tv_title);
tv_overview = mv.findViewById(R.id.tv_overview);
tv_genre = mv.findViewById(R.id.tv_genre);
tv_release_date = mv.findViewById(R.id.tv_release_date);
tv_vote_avg = mv.findViewById(R.id.tv_vote_avg);
tv_vote_count = mv.findViewById(R.id.tv_vote_count);
}
/*Set all the movie data to show*/
@SuppressLint("SetTextI18n")
public void bindData(Movie mMovie){
if(mMovie != null) {
Glide.with(mv).load(APIVars.BASE_IMG_URL mMovie.getBackdrop_path()).into(movie_poster);
tv_title.setText("Title: " mMovie.getTitle());
tv_id.setText("ID: " mMovie.getId());
tv_overview.setText("Overview: " mMovie.getOverview());
tv_genre.setText("Genre: " mMovie.getGenres());
tv_release_date.setText("Release Date: " mMovie.getRelease_date());
tv_vote_count.setText("Vote Count: " mMovie.getVote_count());
tv_vote_avg.setText("Vote AVG: " mMovie.getVote_average());
}
}
}
I don't see anything wrong that could affect performance, but no matter where I put the notifyDataSetChanged function, the result is still that I have to press the button twice for it to display the information.