android – How to add multiple items to RecyclerView on Button Click?-ThrowExceptions

Exception or error:

I’m trying to add details of movies to Recyclerview on Button Click. I have 2 Activities. I can add the details of movies in the first activity and I can view the list of movies in the Second Activity. The first activity contains a submit button to add movies.

enter image description here

But the issue I’m facing is that I can add movies to the first row only. If i try to add another movie it replaces the first row. I want to create a list of movies on button click. How do I do this? The below code is the Main Activity (First Activity).

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setActionBarColour();
        clickEvents();

    }

    private void clickEvents()
    {
        final EditText editTextMovieName = findViewById(R.id.movieName);
        final EditText editTextGenre = findViewById(R.id.genre);
        final EditText editTextReleaseYear = findViewById(R.id.year);

        Button submit = findViewById(R.id.btnSubmit);
        Button movieList = findViewById(R.id.btnMovieList);

        final Bundle bundle = new Bundle();

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editTextMovieName.getText().toString();
                String genre = editTextGenre.getText().toString();
                String year = editTextReleaseYear.getText().toString();

                bundle.putString("MOVIE_NAME", name);
                bundle.putString("GENRE", genre);
                bundle.putString("YEAR", year);
            }
        });

        movieList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MovieListActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void setActionBarColour()
    {
        ActionBar actionBar;
        actionBar = getSupportActionBar();

        ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#3498DB"));
        assert actionBar != null;
        actionBar.setBackgroundDrawable(colorDrawable);
    }
}

This below code is only a function in the Second Activity to get the data

private void prepareMovieData()
    {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        String movieName = bundle.getString("MOVIE_NAME");
        String genre = bundle.getString("GENRE");
        String year = bundle.getString("YEAR");

        Movie movie = new Movie(movieName,genre,year);
        movieList.add(movie);

    }

If I edit the above code to

        Movie movie = new Movie(movieName,genre,year);
        movieList.add(movie);        

        Movie movie2 = new Movie(movieName,genre,year);
        movieList.add(movie2);

I can add only 2 movies. If i want to put 3 movies then I would have to put a third statement like this

        Movie movie3 = new Movie(movieName,genre,year);
        movieList.add(movie3);

The more movies I want the more lines of code I have to write. So how do I put a large number of movies on click?

How to solve:

As per your question detailing we can conclude that

1) On Submit move to next Activity with movie details and add in adapter.

2) Press back

3) Repeat step 1 with new details.

So here revisiting next screen will not have previous data. For that you have to save data in local storage and display.

###

As i review your code when you call the second activity its clear your model data and make its easy use parcable data into model class which help you to pass the model class from one activity to another use onActivityresult for this and add this you model into list to add multiple data through intent. or you can save your data into local sql-lite and fetch data into next activity

Leave a Reply

Your email address will not be published. Required fields are marked *