0

I am trying to display an Empty view in an app that displays movie posters. I am using a recyclerview - for example when I put my device in flight mode and click refresh I was expecting the app to display the empty state view but this is not the case.

The posters are still displayed in the Activity. Why is this the case?

The data is from the moviedatabase api & I am retrieving a movie title & relative image path

MAIN ACTIVITY

    public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Movie>> {
    private static final String LOG_TAG = MainActivity.class.getSimpleName();


    private static final int LOADER_ID = 1;
    private MovieAdapter mMovieAdapter;
    private RecyclerView mRecyclerView;
    private GridLayoutManager mGridLayoutManager;
    private LoaderManager mLoaderManager;
    private View mLoadingIndicator;
    private TextView mEmptyTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLoadingIndicator = findViewById(R.id.loadingIndicator);

        mEmptyTextView = (TextView) findViewById(R.id.emptyStateTextView);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        mLoadingIndicator.setVisibility(View.VISIBLE);


        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mGridLayoutManager = new GridLayoutManager(this, 2);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        mMovieAdapter = new MovieAdapter();
        mRecyclerView.setAdapter(mMovieAdapter);

        mLoaderManager = getSupportLoaderManager();

        mLoaderManager.initLoader(LOADER_ID, null, this);
    }


    public void showMovieDataView() {
        mEmptyTextView.setVisibility(View.INVISIBLE);
        mRecyclerView.setVisibility(View.VISIBLE);
    }

    public void showErrorMessage() {
        mRecyclerView.setVisibility(View.INVISIBLE);
        mEmptyTextView.setVisibility(View.VISIBLE);
    }


    @Override
    public Loader<List<Movie>> onCreateLoader(int id, Bundle args) {

        return new MovieLoader(this, TmdbUrlUtils.BASE_URL);
    }


    @Override
    public void onLoadFinished(Loader<List<Movie>> loader, List<Movie> data) {

        mLoadingIndicator.setVisibility(View.INVISIBLE);

        if (data == null) {

            showErrorMessage();

        } else {

            showMovieDataView();

            mMovieAdapter.setMovieData(data);
        }
    }

    @Override
    public void onLoaderReset(Loader<List<Movie>> loader) {

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        switch (id) {
            case R.id.refresh:
                mLoadingIndicator.setVisibility(View.VISIBLE);
                mMovieAdapter.setMovieData(null);
                getSupportLoaderManager().restartLoader(LOADER_ID, null, this);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

ADAPTER CLASS

    public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

    List<Movie> mMovieList;
    public static final String BASE_POSTER_URL = "http://image.tmdb.org/t/p/w185";

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Context context = holder.mImageView.getContext();

        String imagePath = mMovieList.get(position).getmPosterPath();

        Uri baseUri = Uri.parse(BASE_POSTER_URL);
        Uri.Builder builder = baseUri.buildUpon();
        builder.appendEncodedPath(imagePath);
        String imageUrl = builder.toString();

        Picasso.with(context).load(imageUrl).into(holder.mImageView);
    }

    @Override
    public int getItemCount() {
        if (null == mMovieList) return 0;
        return mMovieList.size();
    }


    public void setMovieData(List<Movie> data) {
        mMovieList = data;
        this.notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageView;

        public ViewHolder(View itemView) {
            super(itemView);

            mImageView = (ImageView) itemView.findViewById(R.id.imagePoster);
        }
    }
}

ACTIVITY_MAIN XML

    <FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f48fb1"
    tools:context="com.example.android.cinemate.MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#d4e157">
    </android.support.v7.widget.RecyclerView>

    <ProgressBar
        android:id="@+id/loadingIndicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="invisible"/>

    <TextView
        android:id="@+id/emptyStateTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#f44336"
        android:text="No network detected!"
        android:visibility="invisible"/>


</FrameLayout>

CUSTOM OBJECT

    public class Movie implements Parcelable {
    public static final Creator<Movie> CREATOR = new Creator<Movie>() {
        @Override
        public Movie createFromParcel(Parcel in) {
            return new Movie(in);
        }

        @Override
        public Movie[] newArray(int size) {
            return new Movie[size];
        }
    };
    private String mTitle;
    private String mPosterPath;


    public Movie(String title, String posterPath) {
        this.mTitle = title;
        this.mPosterPath = posterPath;
    }

    protected Movie(Parcel in) {
        mTitle = in.readString();
        mPosterPath = in.readString();
    }

    public String getmTitle() {
        return mTitle;
    }

    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public String getmPosterPath() {
        return mPosterPath;
    }

    public void setmPosterPath(String mPosterPath) {
        this.mPosterPath = mPosterPath;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(mTitle);
        parcel.writeString(mPosterPath);
    }
}

This is a LOG of what gets called when flight mode is on:

  Refresh selected with Flight Mode on
TEST......MainActivity onCreateLoader() called
TEST.......MovieLoader loadInBackground() called
TEST.......NetworkUtils getDataFromNetwork() called
TEST.......NetworkUtils createUrl() called
TEST.......NetworkUtils makeHttpRequest() called
TEST.......MovieJsonUtils parseJson() called
TEST......MainActivity onLoadFinished() called
TEST......MainActivity showMovieData() called
gavdev
  • 25
  • 1
  • 5

0 Answers0