Ejemplo n.º 1
0
  // Get data back from child activity.
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) {
      return;
    }

    String id = data.getStringExtra(Constants.KEY_ID);
    String subject = data.getStringExtra(Constants.KEY_SUBJECT);
    String body = data.getStringExtra(Constants.KEY_BODY);
    String url = data.getStringExtra(Constants.KEY_URL);
    float rating = data.getFloatExtra(Constants.KEY_RATING, 0);
    boolean isWatched = data.getBooleanExtra(Constants.KEY_IS_WATCHED, false);

    // When returning from Editor Screen, add/update a movie in the list.
    if (requestCode == REQUEST_EDITOR) {
      if (id.equals(Constants.VALUE_NEW_MOVIE)) {
        // New Movie in list.
        Movie movie = new Movie(subject, body, url, rating, isWatched);
        mMovies.add(movie);

        // Add to file.
        FileManager.addToFile(this, FILE_NAME, movie);
      } else {
        int position = data.getIntExtra(Constants.KEY_POSITION, -1);
        // Update the movie in list at id position.
        Movie movie = mMovies.get(position);
        movie.setSubject(subject);
        movie.setBody(body);
        movie.setUrl(url);
        movie.setRating(rating);
        movie.setIsWatched(isWatched);

        // Rewrite the file.
        FileManager.saveFile(this, FILE_NAME, mMovies);
      }
    }
    // On returning from Search screen, add new movie in the list.
    else if (requestCode == REQUEST_SEARCH) {
      // New Movie in list.
      Movie movie = new Movie(id, subject, body, url);
      movie.setRating(rating);
      mMovies.add(movie);

      // Add to file.
      FileManager.addToFile(this, FILE_NAME, movie);
    }

    mMainListAdapter.notifyDataSetChanged();
  }
Ejemplo n.º 2
0
  // On open application create listview, load movie data from file and
  // show it.
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Loading movie list from the file, if failed - reset the array list.
    if (FileManager.loadFile(this, FILE_NAME, mMovies) == FileManager.RESULT_ERROR) {
      mMovies = new ArrayList<>();
    }

    mListViewMovies = (ListView) findViewById(R.id.listViewMainMovies);
    mEmptyMainMovieList = (TextView) findViewById(R.id.emptyMainMovieList);
    mTextViewMainTopic = (TextView) findViewById(R.id.textViewMainTopic);

    // ListView adapter initialising.
    mMainListAdapter = new MainListAdapter(this, mMovies);
    mListViewMovies.setAdapter(mMainListAdapter);

    // On Item click transferring to Editor Screen.
    mListViewMovies.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            transferToEditorActivity(position);
          }
        });

    // On Item Long click, open menu for edit and delete options.
    mListViewMovies.setOnItemLongClickListener(
        new AdapterView.OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(
              AdapterView<?> parent, View view, final int position, long id) {
            PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
            popupMenu.setOnMenuItemClickListener(
                new PopupMenu.OnMenuItemClickListener() {
                  @Override
                  public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        // Delete the movie from list and file.
                      case R.id.action_delete:
                        // Open confirmation dialog.
                        new AlertDialog.Builder(MainActivity.this)
                            .setTitle(R.string.dialog_delete_movie_title)
                            .setMessage(R.string.dialog_delete_all_message)
                            .setPositiveButton(
                                R.string.dialog_delete_movie_positive_button_label,
                                new DialogInterface.OnClickListener() {
                                  @Override
                                  public void onClick(DialogInterface dialog, int which) {
                                    mMovies.remove(position);
                                    FileManager.saveFile(MainActivity.this, FILE_NAME, mMovies);
                                    mMainListAdapter.notifyDataSetChanged();
                                  }
                                })
                            .setNegativeButton(R.string.button_cancel_label, null)
                            .create()
                            .show();

                        break;

                        // Go to Editor activity.
                      case R.id.action_edit:
                        transferToEditorActivity(position);
                        break;
                    }

                    return true;
                  }
                });
            popupMenu.inflate(R.menu.menu_item);
            popupMenu.show();

            return true;
          }
        });

    mListViewMovies.setEmptyView(mEmptyMainMovieList);
  }