Skip to content

Instantly share code, notes, and snippets.

@parahall
Created May 12, 2017 10:25
Show Gist options
  • Save parahall/0f8eb52c9ac49e03d626fe0198abdcc5 to your computer and use it in GitHub Desktop.
Save parahall/0f8eb52c9ac49e03d626fe0198abdcc5 to your computer and use it in GitHub Desktop.
public class MainActivity extends LifecycleActivity implements Observer<List<StarWarsMovie>> {
@BindView(R.id.lv_am_movie_list) ListView listView;
@BindView(R.id.pb_am_loading) ProgressBar progressBar;
private MainViewModel mainViewModel;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
ButterKnife.bind(this);
listView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
subscribeUiUpdates();
}
private void subscribeUiUpdates() {
mainViewModel.getMoviesLiveData().observe(this, this);
}
@Override public void onChanged(@Nullable List<StarWarsMovie> starWarsMovies) {
Log.d("XXX","Data changed");
MovieAdapter movieAdapter = new MovieAdapter(this, starWarsMovies);
listView.setAdapter(movieAdapter);
progressBar.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
}
static class ViewHolder {
@BindView(R.id.tv_im_movie_name) TextView tvName;
@BindView(R.id.tv_im_movie_description) TextView tvDescription;
@BindView(R.id.tv_im_movie_date) TextView tvDate;
@BindView(R.id.iv_im_movie_poster) ImageView ivPoster;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
private class MovieAdapter extends ArrayAdapter<StarWarsMovie> {
public MovieAdapter(Context context, List<StarWarsMovie> users) {
super(context, 0, users);
}
@NonNull @Override public View getView(int position, View convertView, ViewGroup parent) {
StarWarsMovie starWarsMovie = getItem(position);
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_movie, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.tvName.setText(starWarsMovie.getName());
holder.tvDescription.setText(starWarsMovie.getDescription());
holder.tvDate.setText(starWarsMovie.getReleaseDate());
Glide.with(getContext())
.load(starWarsMovie.getImageUrl())
.placeholder(R.drawable.placeholder)
.into(holder.ivPoster);
return convertView;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment